核心数据从模拟器迁移到设备

时间:2010-10-06 21:39:44

标签: iphone core-data

我正在使用Core Data,我的应用程序在模拟器中完美运行,但在实际设备上数据库是空的。

我启用了CoreData日志记录,因此我看到正在创建的实际表,并且查询运行但所有查询都为空。我还从设备中复制了应用程序并验证了架构已创建但没有数据。

实际重新填充的数据我缺少什么?

更新代码

- (NSDictionary*)migrationOptions {
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];   
    return options;
}

- (NSManagedObjectContext*)managedObjectContext {
    if( _managedObjectContext != nil ) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator: coordinator];
        [_managedObjectContext setUndoManager:nil];
        [_managedObjectContext setRetainsRegisteredObjects:YES];
    }
    return _managedObjectContext;
}


- (NSManagedObjectModel*)managedObjectModel {
    if( _managedObjectModel != nil ) {
        return _managedObjectModel;
    }

    NSString *path = [[NSBundle mainBundle] pathForResource:@"MyApp" ofType:@"momd"];
    NSURL *momURL = [NSURL fileURLWithPath:path];

    //_managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];

    return _managedObjectModel;
}


- (NSPersistentStoreCoordinator*)persistentStoreCoordinator {
    if( _persistentStoreCoordinator != nil ) {
        return _persistentStoreCoordinator;
    }

    NSString* storePath = [self storePath];
    NSURL *storeUrl = [self storeUrl];


    NSError* error;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

    NSDictionary* options = [self migrationOptions];

    // Check whether the store already exists or not.
    NSFileManager* fileManager = [NSFileManager defaultManager];
    BOOL exists = [fileManager fileExistsAtPath:storePath];

    TTDINFO(storePath);
    if( !exists ) {
        _modelCreated = YES;
    } else {
        if( _resetModel ||
           [[NSUserDefaults standardUserDefaults] boolForKey:@"erase_all_preference"] ) {
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"erase_all_preference"];
            [fileManager removeItemAtPath:storePath error:nil];
            _modelCreated = YES;
        }
    }

    if (![_persistentStoreCoordinator
          addPersistentStoreWithType: kStoreType
          configuration: nil
          URL: storeUrl
          options: options
          error: &error
          ]) {
        // We couldn't add the persistent store, so let's wipe it out and try again.
        [fileManager removeItemAtPath:storePath error:nil];
        _modelCreated = YES;

        if (![_persistentStoreCoordinator
              addPersistentStoreWithType: kStoreType
              configuration: nil
              URL: storeUrl
              options: nil
              error: &error
              ]) {
            // Something is terribly wrong here.
        }
    }

    return _persistentStoreCoordinator;
}


- (NSString*)storePath {
    return [[self applicationDocumentsDirectory] stringByAppendingPathComponent: kStoreFilename];
}


- (NSURL*)storeUrl {
    return [NSURL fileURLWithPath:[self storePath]];
}

2 个答案:

答案 0 :(得分:0)

显示设置Core Data的代码,尤其是托管对象上下文。如果要在应用程序包中创建后备存储,这将在模拟器上成功,但这将在设备上失败。 (作为可能出错的一个例子。)

答案 1 :(得分:0)

好的,所以经过这么多搜索后终于得到了解决方案。

基本上是将原始sql文件从模拟器复制到设备。

来自http://iphonedevelopment.blogspot.com/2010/08/core-data-starting-data.html

这是Persistant Store Coordinator的创建:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{
    @synchronized (self)
    {
        if (persistentStoreCoordinator != nil)
            return persistentStoreCoordinator;

        NSString *defaultStorePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"My_App_Name" ofType:@"sqlite"];
        NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"My_App_Name.sqlite"];

        NSError *error;
        if (![[NSFileManager defaultManager] fileExistsAtPath:storePath]) 
        {
            if ([[NSFileManager defaultManager] copyItemAtPath:defaultStorePath toPath:storePath error:&error])
                NSLog(@"Copied starting data to %@", storePath);
            else 
                NSLog(@"Error copying default DB to %@ (%@)", storePath, error);
        }

        NSURL *storeURL = [NSURL fileURLWithPath:storePath];

        persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

        if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) 
        {

            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }    

        return persistentStoreCoordinator;
    }    
}