Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger sync #172

Open
teawithfruit opened this issue Dec 28, 2021 · 1 comment
Open

Trigger sync #172

teawithfruit opened this issue Dec 28, 2021 · 1 comment

Comments

@teawithfruit
Copy link

What would you suggest for trigger a sync from a class which is not AppDelegate?
I'm not sure if I should create a shared instance or use the NotificationCenter or if I have to import SyncKit and create an instance from CloudKitSynchronizer in every class where I interact with Realm.

Your examples do not show something like this.
I would appriciate, if you have a best practice for that.

@mentrena
Copy link
Owner

Hey @teawithfruit, generally you want to create one synchronizer per model, so if you're using Core Data you would have one synchronizer for your managed object context, and if you're using Realm you would have one synchronizer for your entire realm.

I would recommend creating your synchronizer once (I use the AppDelegate in the example), then pass it to each controller that needs it using dependency injection (here), so they can trigger synchronisation.

In another app of my own I use a protocol like this

protocol SynchronizationInteractor: AnyObject {
    var isSynchronizationEnabled: Bool { get }
    var isSynchronizing: Bool { get }
    var error: Error? { get }
    func synchronize(completion: ((Error?) -> ())?)
    func cancel()
}

And then implement it in a class that owns the synchronizer but doesn't expose it. So the rest of the app is fully agnostic to the technology that's being used to synchronize data.

In the case of the example app it uses VIPER and sets up all dependencies in the App Delegate, but I could give more specific advice depending on how you're architecting your app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants