我有 2 个应用程序,一个免费,一个付费。我设置了这两个应用程序,以便人们可以使用以下代码将他们的数据从免费传输到付费
在免费的(在视图控制器中)
- (IBAction)exportToPro:(id)sender
{
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Medicine_Tracker.sqlite"];
NSString *storeURLString = [storeURL path];
if( [[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]] ) NSLog(@"Exists");
NSData *fileData = [NSData dataWithContentsOfFile:storeURLString];
NSString *encodedString = [GTMBase64 stringByWebSafeEncodingData:fileData padded:YES];
NSString *urlString = [NSString stringWithFormat:@"mt://localhost/importDatabase?%@", encodedString];
NSURL *openURL = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:openURL];
}
并在付费中(在应用程序委托中)
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
if([@"/importDatabase" isEqual:[url path]]) {
NSString *query = [url query];
NSData *importUrlData = [GTMBase64 webSafeDecodeString:query];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Medicine_Tracker.sqlite"];
NSString *storeURLString = [storeURL path];
if( [[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]] ) NSLog(@"Exists");
// NOTE: In practice you will want to prompt the user to confirm before you overwrite their files!
[importUrlData writeToFile:storeURLString atomically:YES];
return YES;
}
return NO;
}
现在这段代码有效,但我必须关闭应用程序并重新启动它才能加载新的核心数据。我如何做到这一点,这样您就不必关闭并重新打开应用程序来显示新数据,我尝试在表格上使用重新加载数据,但它仍然显示旧记录
我认为问题出在已经设置持久存储之后调用 handleOpenURL 方法的事实
openurl 方法中的新代码
__persistentStoreCoordinator = nil;
__managedObjectContext = nil;
__managedObjectModel = nil;
NSLog(@"PSC: %@ - MOC: %@ - MOM: %@", __persistentStoreCoordinator, __managedObjectContext, __managedObjectModel);
NSError *error = nil;
__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]);
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Medicine_Tracker" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
}
NSLog(@"PSC: %@", __persistentStoreCoordinator);
NSLog(@"MOC: %@", __managedObjectContext);
NSLog(@"MOM: %@", __managedObjectModel);