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

Cancel sync process #540

Closed
zoltanbognar opened this issue Oct 5, 2018 · 6 comments
Closed

Cancel sync process #540

zoltanbognar opened this issue Oct 5, 2018 · 6 comments

Comments

@zoltanbognar
Copy link

Hi Guys

Is there a way to cancel a syncing process? I haven't found it in the documentation.
I'm syncing a downloaded json to core data with DataStack. it can take a while, when json has lets say 50 entities.
e.g when user logs out from the app I'm deleting his data from database, but when he logs out during sync and the sync is finished after he logged out, then the deleted data is unfortunatly back in the database.
So anyway I want to cancel syncing when user logs out.

Thanks for help

Zoltan

@3lvis
Copy link
Owner

3lvis commented Oct 5, 2018

Hi! Sync uses (NS)Operations, so if you make an OperationQueue and add sync to it then you can cancel it at any point.

@3lvis
Copy link
Owner

3lvis commented Oct 5, 2018

I'll see if I can add some docs later, but here are you see Sync is a subclass of Operation:

@objc public class Sync: Operation {

@zoltanbognar
Copy link
Author

zoltanbognar commented Oct 11, 2018

Hi Thanks for help
I implemented the cancel through OperationQueue.
something like:
`var queue: OperationQueue = OperationQueue()

let sync = Sync(changes: changes, inEntityNamed: entityName, dataStack: dataStack, operations: syncOptions)

queue.addOperations([sync], waitUntilFinished: false)`

Only one thing i missing:
I'm not getting error feedback like in the datastack static sync method.
In the sync perform method error is just printed to the console

Best

Zoltan

@3lvis
Copy link
Owner

3lvis commented Oct 12, 2018

Hi Zoltan,

You're right, error handling wasn't 100% finished, I'm using try and throws as much as possible but for the cases where this can't be used (closures and overriding methods) I haven't been able to find the time to complete error handling.

I think we might need to introduce a delegate to the operation in order to inform what failed, or do you have another suggestion in how to inform about an error from inside an operation. :)

Best

@zoltanbognar
Copy link
Author

zoltanbognar commented Oct 12, 2018

Hi Elvis

one approach can be to add public error variable to Sync class:

public var error: NSError?

and than:

func perform(using context: NSManagedObjectContext) {
        do {
            try Sync.changes(self.changes, inEntityNamed: self.entityName, predicate: self.predicate, parent: self.parent, parentRelationship: self.parentRelationship, inContext: context, operations: self.filterOperations, shouldContinueBlock: { () -> Bool in
                return !self.isCancelled
            }, objectJSONBlock: { objectJSON -> [String: Any] in
                return self.delegate?.sync(self, willInsert: objectJSON, in: self.entityName, parent: self.parent) ?? objectJSON
            })
        } catch let error as NSError {
            print("Failed syncing changes \(error)")
            self.error = error
        }

        self.updateExecuting(false)
        self.updateFinished(true)
    }

in the code user can check for e.g. error like this:

let sync = Sync(changes: changes, inEntityNamed: entityName, dataStack: dataStack, operations: syncOptions)
sync.completionBlock = { [unowned sync] in
    if let completion = completion {
          completion(sync.error)
     }
}

but maybe there is better aproach

Best

Zoltan

@3lvis
Copy link
Owner

3lvis commented Feb 11, 2020

Thanks for your support in this library. It has been a while since I used CoreData in any of my projects and it's hard for me to maintain this library. If you have been part of the Sync contributors group and want to take ownership of the library let me know to elvisnunez@me.com. For now, I'll be archiving this library.

@3lvis 3lvis closed this as completed Feb 11, 2020
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