0

I have succesfuly integrated Ensembles Framework in my App (Swift 3) with DropBox (API V2). Initialy I have used Icloud but finaly I prefer Dropbox.

Link to Ensembles on Github

The sync works but only if I add some objects. (My objects are some movies)

For example in the simulators, if in the first device I add a movie, then run my app on a second device I can see that movie after the sync.

But if in the first device I delete a movie, that one is not deleted on the second device and when I run my app again on the first device that movie come back. So I can't delete any object !

I suppose I do something wrong.

Here is my code when I delete a movie: (Classic core data deleting method I think?)

 //Delete a movie
    class func EffacerTitreDansBaseFilmsViaID(IdFilm:Int32){

        let fetchRequest:NSFetchRequest <BaseFilms> = BaseFilms.fetchRequest()

        let predicate = NSPredicate(format: "id = %d",IdFilm)
        fetchRequest.predicate=predicate

        let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest as! NSFetchRequest<NSFetchRequestResult>)


        do {
            try managedObjectContext.execute(deleteRequest)
        }catch{
            print(error.localizedDescription)
        }
        //I also save again the context:(Just to be sure...)
        do {
            try managedObjectContext.save()
        } catch {
            print(error.localizedDescription)
        }

    }

Here is the two observers that are in my AppDelegate :

 // Listen for local saves, and trigger merges
NotificationCenter.default.addObserver(self, selector:#selector(self.localSaveOccurred(_:)), name:NSNotification.Name.CDEMonitoredManagedObjectContextDidSave, object:nil)
NotificationCenter.default.addObserver(self, selector:#selector(self.cloudDataDidDownload(_:)), name:NSNotification.Name.CDEICloudFileSystemDidDownloadFiles, object:nil)

And the functions associated:

func localSaveOccurred(_ notif: Notification) {
            self.synchroniseCloudDatas(nil)
        }


func cloudDataDidDownload(_ notif: Notification) {
            self.synchroniseCloudDatas(nil)
        }

And the sync function:

func synchroniseCloudDatas(_ completion: (() -> Void)?) {
            if !ensemble.isLeeched {
                ensemble.leechPersistentStore {
                    error in
                    completion?()
                }
            }
            else {
                ensemble.merge {
                    error in
                    completion?()
                }
            }
        }

Does any body have an idea about my mistakes?

EDIT:

Can this problemn occurs becauses of the relationships between my objects? (One to many, ...)

When I create a relationship I think that there are no global identifier in the ensembles framework?

The global Identifier are only setup for the Entities, correct? With this method:

 func persistentStoreEnsemble(_ ensemble: CDEPersistentStoreEnsemble!, globalIdentifiersForManagedObjects objects: [Any]!) -> [Any]! {
        print("%@", (objects as NSArray).value(forKeyPath: "uuid") as! [AnyObject])
        return (objects as NSArray).value(forKeyPath: "uuid") as! [AnyObject]
    }

Thank you in advance.

Fabien
  • 1
  • 1

1 Answers1

0

I think the problem is simply that you are using a batch delete. Batch operations are not supported with Ensembles, because there is no way for Ensembles to observe the save changes. If you use a standard delete, by taking the movie object and calling deleteObject: on the NSManagedObjectContext, it will sync properly.

Drew McCormack
  • 3,490
  • 1
  • 19
  • 23