nothirst / TICoreDataSync

Automatic synchronization for Core Data apps, between any combination of Mac OS X and iOS: Mac to iPhone to iPad to iPod touch and back again.
https://github.com/nothirst/TICoreDataSync/wiki
807 stars 61 forks source link

UploadWholeStore problem, empty db #95

Closed renzos closed 10 years ago

renzos commented 10 years ago

I succesfully set up TICoreDataSync with my iOS project and it synchronizes very well between devices. Then I put a UIButton on the main ViewController in order to Upload the Whole Store everytime the user wants. Looking at the log the Store seems to be uploaded succesfully but if I open with an Sqlite browser the "WholeStore.ticdsync" uploaded on dropbox it has the correct model structure but no records inside... before doing the upload I tried to synchronize but the outcome is the same. And then when I Download the store I have this strange behaviour in the log:

-[TICDSWholeStoreDownloadOperation beginDownloadOfAppliedSyncChangeSetsFile] : Downloading applied sync change sets file to file:///Users/myname/Library/Application䌼ɘތȀ䓤睯汮慯楤杮愠灰楬摥猠湹⁣ [many lines of japanese characters...]

this does not appear on the real device but the upload problem persists

renzos commented 10 years ago

It seems that the new Write-Ahead-Logging (WAL) of IOS 7 is causing problems because it splits up the sqlite database in 3 files:

I tried to disable it with this option but it still creates 3 files: NSDictionary *options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} }; Any suggestion?

pventura1976 commented 10 years ago

How do you build the Core Data stack?

renzos commented 10 years ago

Here how I build the Core Data stack, even if I set up the option "NSDictionary *options = @{NSSQLitePragmasOption:@{@"journal_mode":@"DELETE"}};" it still creates 3 files. Reading the specification of the WriteAheadLogging (https://www.sqlite.org/wal.html) the original content is preserved in the database file and the changes are appended into the separate WAL file, so when I launch the app for the first time the Core Data Stack is build, the persistentStore.sqlite is created but it's empty and every time I add, delete or modify a record, changes are stored in the persistentStore.sqlite-wal file. So when I upload the Whole Store this is always empty:

#pragma mark - Core Data stack

// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return _managedObjectContext;
}

// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"AppName" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}

// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"persistentStore.sqlite"];

    /* Add the check for an existing store here... */
    if ([[NSFileManager defaultManager] fileExistsAtPath:storeURL.path] == NO) {
        self.downloadStoreAfterRegistering = NO;
    }

    NSError *error = nil;

    NSDictionary *options = @{NSSQLitePragmasOption:@{@"journal_mode":@"DELETE"}};

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 

         Typical reasons for an error here include:
         * The persistent store is not accessible;
         * The schema for the persistent store is incompatible with current managed object model.
         Check the error message to determine what the actual problem was.

         If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.

         If you encounter schema incompatibility errors during development, you can reduce their frequency by:
         * Simply deleting the existing store:
         [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

         * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
         @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}

         Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        return nil;
    }
    return _persistentStoreCoordinator;
}
renzos commented 10 years ago

It seems that the error was here because I didn't previuosly set the pragmaOptions also here but just in the main Core Data Stack:

- (void)documentSyncManager:(TICDSDocumentSyncManager *)aSyncManager didReplaceStoreWithDownloadedStoreAtURL:(NSURL *)aStoreURL{
NSError *anyError = nil;
    NSMutableDictionary *pragmaOptions = [NSMutableDictionary dictionary];
    [pragmaOptions setObject:@"delete" forKey:@"journal_mode"];
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                             pragmaOptions, NSSQLitePragmasOption,
                             nil];

    id store = [self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:aStoreURL options:options error:&anyError];

    if (store == nil) {
        NSLog(@"Failed to add persistent store at %@: %@", aStoreURL, anyError);
    }
}
kevinhoctor commented 10 years ago

So did this remove the error for you?

Also, you can simplify your dictionary notation now in Objective-C:

NSDictionary *options = @{ NSMigratePersistentStoresAutomaticallyOption : @YES, NSInferMappingModelAutomaticallyOption : @YES, NSSQLitePragmasOption : @{ @"journal_mode" : @"DELETE" } };

I find the new notation much easier to read and understand as well.

renzos commented 10 years ago

It seems that WAL has been completely switched off but I'll make more tests tomorrow to confirm that, maybe it should be added to the documentation since WAL is ON by default in iOS7. I'll also simplified the dictionary notation as you suggested and it's ok, much easier to read, thanks.

renzos commented 10 years ago

I confirm I removed the problem as I told above, so summarizing it I had to set the pragmaOptions both in the Core Data stack and in the didReplaceStoreWithDownloadedStoreAtURL delegate method.