Skip to content

Easy File Syncing between iOS, MacOS and tvOS using CloudKit

License

Notifications You must be signed in to change notification settings

iOS-Swift-Controls/FileSinki

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FileSinki

Easy file syncing between iOS, MacOS and tvOS, using CloudKit.

Setup

Usage

FileSyncable

Adopt the FileSyncable protocol to get automatic coding and decoding of your data.

struct SaveGame: FileSyncable {
    let score: Double

    func shouldOverwrite(other: Self) -> Bool {
        return score > other.score
    }
}

If your struct / class already conforms to Comparable, shouldOverwrite by default overwrites if self > other

Saving, Loading and Deleting

// load a SaveGame from a file with path: "SaveGames/player1.save"
FileSinki.load(SaveGame.self,
               fromPath: "SaveGames/player1.save") { saveGame, wasRemote in
    // closure *may* be called multiple times, 
    // if the cloud has a better version of saveGame
}

// save a saveGame to a file with path: "SaveGames/player1.save"
FileSinki.save(saveGame,
               toPath: "SaveGames/player1.save") { finalVersion in
    // closure *may* be called with finalVersion 
    // if the saveGame changed as a result of a merge
    // or a better available version
}

// delete the saveGame
FileSinki.delete(saveGame, at: "SaveGames/player1.save")

Mergables

Adopt the FileMergable and implement merge(with:) to merge FileSyncables between devices. Return the new merged object / struct which will be used.

struct SaveGame: FileSyncable, FileMergable {
    let trophies: [Trophy]

    func merge(with other: Self) -> Self? {
        let combinedTrophies = (trophies + other.trophies).sorted()
        return SaveGame(trophies: combinedTrophies)
    }
}

If you return nil from merge(with:) then FileSinki falls back to shouldOverwrite(other:)

About

Easy File Syncing between iOS, MacOS and tvOS using CloudKit

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 95.0%
  • Ruby 4.6%
  • Objective-C 0.4%