Har*_*pta 6 iphone xcode synchronization core-data ios
我正在开发一个ipad应用程序,我正在处理核心数据.
应用程序管理的数据可以分为两类.
所以在scnerio中,我想到在我的项目中有两个模型文件和两个corressponding sqlite文件.并同步一个sqlite文件以实现syching.
如果我的方法是正确和可行的,请建议.如果没有,那么请建议其他解决方案.
嘿伙计们,请尝试理解这个问题.这里我说的是两个sqlite文件彼此具有不同的结构.表示".xcdatamodel"模型文件
谢谢.
Sat*_*ran 18
这里可能重复.
您可以拥有任意数量的数据模型,前提是您为每个模型创建不同的托管对象上下文并正确管理它们.
- (NSURL *)applicationDocumentsDirectoryForCoreData
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
Run Code Online (Sandbox Code Playgroud)
//第一个数据模型
NSURL *modelURL1 = [[NSBundle mainBundle] URLForResource:@"1_model" withExtension:@"momd"];
NSURL *storeURL1 = [[self applicationDocumentsDirectoryForCoreData] URLByAppendingPathComponent:@"1_model.sqlite"];
NSError *error = nil;
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL1];
persistentStoreCoordinator1 = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel];
if (![persistentStoreCoordinator1 addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL1 options:nil error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
Run Code Online (Sandbox Code Playgroud)
//第二个模型
NSURL *modelURL2 = [[NSBundle mainBundle] URLForResource:@"2_model" withExtension:@"momd"];
NSURL *storeURL2 = [[self applicationDocumentsDirectoryForCoreData] URLByAppendingPathComponent:@"2_model.sqlite"];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL2];
NSError *error = nil;
persistentStoreCoordinator2 = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
if (![persistentStoreCoordinator2 addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL2 options:nil error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
Run Code Online (Sandbox Code Playgroud)
在为您想要的商店取出MOC时:
//select your store - do that in selectStore or a function like that.
NSPersistentStoreCoordinator *coordinator = [self selectStore];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:coordinator];
}
Run Code Online (Sandbox Code Playgroud)
两家商店之间的选择.
-(NSPersistentStoreCoordinator *)selectStore
{
if(someCondtion? return persistentStoreCoordinator1: persistentStoreCoordinator2;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7037 次 |
| 最近记录: |