lhunath / UbiquityStoreManager

Implements Core Data + iCloud, deals with all the nasty stuff and gives you a clean API.
http://lhunath.github.io/UbiquityStoreManager
Apache License 2.0
391 stars 37 forks source link

Processing before didImportChanges: merges and saves #37

Closed neilt closed 10 years ago

neilt commented 10 years ago

My project has two core data models, one that is iCloud synched, and the other which is local only, both in the same context. I need to update some things in the local only model when data changes in iCloud model.

In looking at the following from UbiquityStoreManager.m it looks like I would not get the USMStoreDidImportChangesNotification until after deleted managed objects were already removed from the persistent store?

- (void)didImportChanges:(NSNotification *)note {

    NSManagedObjectContext *moc = nil;
    if ([self.delegate respondsToSelector:@selector(managedObjectContextForUbiquityChangesInManager:)])
        moc = [self.delegate managedObjectContextForUbiquityChangesInManager:self];
    if (moc) {
        [self log:@"Importing ubiquity changes into application's MOC.  Changes:\n%@", note.userInfo];
        [moc performBlockAndWait:^{
            [moc mergeChangesFromContextDidSaveNotification:note];

            NSError *error = nil;
            if ([moc hasChanges] && ![moc save:&error]) {
                [self error:error cause:UbiquityStoreErrorCauseImportChanges context:note];
                [self reloadStore];
                return;
            }
        }];
    }
    else
        [self log:@"Application did not specify an import MOC, not importing ubiquity changes:\n%@", note.userInfo];

    dispatch_async( dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:USMStoreDidImportChangesNotification
                                                            object:self userInfo:[note userInfo]];
    } );
}

If I register for NSPersistentStoreDidImportUbiquitousContentChangesNotification directly how do I know that I received this notifications before UbiquityStoreManager has merged the changes and saved?

Is it possible to get a delegate call at the beginning of didImportChanges:. Something like:

    if ([self.delegate respondsToSelector:@selector(willMergeAndSaveImportChangesFromNote:)])
        [self.delegate willMergeAndSaveImportChangesFromNote:note];

This way I can save what I need to process from the notifications, multithreaded aware, for later processing before the data goes away or changes.

Thank you. Neil

lhunath commented 10 years ago

In 6f959da I've modified the delegate method to:

- (NSManagedObjectContext *)ubiquityStoreManager:(UbiquityStoreManager *)manager
          managedObjectContextForUbiquityChanges:(NSNotification *)note;

You should now be able to do your custom logic before returning the MOC to USM.

neilt commented 10 years ago

Wow, excellent! When to lunch and when I returned the change was made.

Thank you. Neil

lhunath commented 10 years ago

That's because I made it during my lunch time ;-)