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

Parent not assigned when Sync is used as operation #499

Closed
Jonge opened this issue Apr 24, 2018 · 2 comments
Closed

Parent not assigned when Sync is used as operation #499

Jonge opened this issue Apr 24, 2018 · 2 comments

Comments

@Jonge
Copy link

Jonge commented Apr 24, 2018

Hey again @3lvis,

Just updated to latest Sync from an old version and it seems that syncing parent with operations is again broken.

Here are the test cases: https://github.com/Jonge/Sync/tree/bug-operation-parent

Method testParentWithOperation() fails, while testParentWithoutOperation() succeeds. They are the same apart from using Sync as operation vs changes(...).

Not sure yet what could be wrong, I will keep digging, but you might be faster than me. 😉

@Jonge
Copy link
Author

Jonge commented Apr 24, 2018

OK so the issue is that parentRelationship is nil and is not automatically created.

The workaround is to do something like this in the perform(...) method in Sync.swift. (Which is ugly but works.)

    func perform(using context: NSManagedObjectContext) {
        do {
            if let parent = parent, parentRelationship == nil {
                let safeParent = parent.sync_copyInContext(context)
                guard let entity = NSEntityDescription.entity(forEntityName: entityName, in: context) else { fatalError("Couldn't find entity named: \(entityName)") }
                let relationships = entity.relationships(forDestination: parent.entity)
                var predicate: NSPredicate?
                let firstRelationship = relationships.first
                
                if let firstRelationship = firstRelationship {
                    predicate = NSPredicate(format: "%K = %@", firstRelationship.name, safeParent)
                }
                
                try Sync.changes(self.changes, inEntityNamed: self.entityName, predicate: predicate, parent: safeParent, parentRelationship: firstRelationship?.inverseRelationship, 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
                })
            } else {
                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.updateExecuting(false)
        self.updateFinished(true)
    }

I'm not sure how we want to behave when for instance:

  • a predicate is already set,
  • both parent relationship and parent are set,
  • only parent relationship is set.

@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