I am trying to load my pre-populated into a WatchKit enabled app. The files copy without error, but the data returns blank in the app.
I have created a AppKit.framework to manage my managedObjectContext in my App Group, as in this sample application: http://ift.tt/1Kl4jOK
The preloaded .sqlite file is not empty, and it copies to the correct directory without error, but when the app opens, everything is blank.
Here is my DataAccess file in my framework:
- (NSURL *)applicationDocumentsDirectory
{
// The directory the application uses to store the Core Data store file. This code uses a directory named "com.gaiam.MyApp" in the applications documents directory.
return [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.company.myapp"];
}
- (NSURL *)storeURL{
return [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyApp.sqlite"];
}
- (NSURL *)newStoreURL{
return [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyApp.sqlite"];
}
- (NSURL *)walURL {
return [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyApp.sqlite-wal"];
}
- (NSURL *)shmURL {
return [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyApp.sqlite-shm"];
}
- (NSURL *)modelKitURL {
return [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyApp.momd"];
}
- (NSURL *)modelURL{
return [[NSBundle mainBundle] URLForResource:@"MyApp" withExtension:@"momd"];
//return [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyApp.momd"];
}
- (NSManagedObjectModel *)managedObjectModel
{
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[self modelURL]];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesnt exist, copy the default store.
if (![fileManager fileExistsAtPath:[[self storeURL] path]]) {
NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"MyApp" withExtension:@"sqlite"];
if (defaultStoreURL) {
NSError *error;
[fileManager copyItemAtURL:defaultStoreURL toURL:[self storeURL] error:&error];
if (error)
NSLog(@"errorDescription %@", error.description);
}
}
if (![fileManager fileExistsAtPath:[[self walURL] path]]) {
NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"MyApp" withExtension:@"sqlite-wal"];
if (defaultStoreURL) {
NSError *error;
[fileManager copyItemAtURL:defaultStoreURL toURL:[self walURL] error:&error];
if (error)
NSLog(@"errorDescription %@", error.description);
}
}
if (![fileManager fileExistsAtPath:[[self shmURL] path]]) {
NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"MyApp" withExtension:@"sqlite-shm"];
if (defaultStoreURL) {
NSError *error;
[fileManager copyItemAtURL:defaultStoreURL toURL:[self shmURL] error:&error];
if (error)
NSLog(@"errorDescription %@", error.description);
}
}
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[self storeURL] options:@{ NSMigratePersistentStoresAutomaticallyOption : @YES,
NSInferMappingModelAutomaticallyOption : @YES }
error:&error]) {
// Report any error we got.
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
dict[NSLocalizedFailureReasonErrorKey] = failureReason;
dict[NSUnderlyingErrorKey] = error;
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
// Replace this 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.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
Again, I know the .sqlite included in my bundle is not empty, and I have it's target set to both my framework and app. I have tried including the files in both the framework and app bundles. No luck.
Aucun commentaire:
Enregistrer un commentaire