我正在做一个使用Core Data Framework的应用程序,但它只能在iPhone模拟器中运行。我知道真正的动机:核心数据的文件保存在iPhone模拟器目录中。我想改变它,我采用sqlite类型的存档并放入Xcode(支持文件),我想加载其中包含的数据。我是怎么做到的?
我的代码:
#pragma core data methods
-(NSManagedObjectModel *)managedObjectModel
{
if(managedObjectModel!=nil)
{
return managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle]URLForResource:@"iGyn" withExtension:@"momd"];
managedObjectModel = [[NSManagedObjectModel alloc]initWithContentsOfURL:modelURL];
return managedObjectModel;
}
-(NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if(persistentStoreCoordinator!=nil)
{
return persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory]URLByAppendingPathComponent:@"iGyn.sqlite"];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:[self managedObjectModel]];
if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{
NSLog(@"error...");
}
return persistentStoreCoordinator;
}
-(NSManagedObjectContext *)managedObjectContext
{
if(managedObjectContext!=nil)
{
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if(coordinator!=nil)
{
managedObjectContext = [[NSManagedObjectContext alloc]init];
[managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return managedObjectContext;
}
-(NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager]URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject];
}
-(void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *moc = self.managedObjectContext;
if(moc!=nil)
{
if([moc hasChanges] && ![moc save:&error])
{
NSLog(@"error...");
}
}
}
我应该更改哪些核心数据才能在我的设备上运行?我已经将数据保存在我的sqlite文件中。