2015-01-05 31 views
1

我正在更新Apple Watch的應用程序 此應用程序使用coredata,所以我創建了一個框架來管理Core Data堆棧! 當我運行在設備或模擬器的應用做工精細的應用程序,但是當我與此日誌managedObjectModel in nil(僅適用於WatchApp)

***終止應用程序在蘋果手錶模擬器中運行的應用程序崩潰是由於未捕獲的異常「NSInvalidArgumentException」,原因:

的問題似乎是managedObjectModel「不能用零模型創建 NSPersistentStoreCoordinator」,如果我登錄它

NSLog(@"managedObjectModel %@", _managedObjectModel); 

日誌重轉

managedObjectModel(空)

在框架的代碼似乎是正確的其實主要的應用程序完美的作品

反正這是框架

#import "DataAccess.h" 

@implementation DataAccess 

@synthesize managedObjectContext = _managedObjectContext; 
@synthesize managedObjectModel = _managedObjectModel; 
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator; 


+ (instancetype)sharedInstance 
{ 
    static DataAccess *sharedInstance = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     sharedInstance = [[DataAccess alloc] init]; 
     // Do any other initialisation stuff here 
    }); 
    return sharedInstance; 
} 

- (void)saveContext 
{ 
    NSError *error = nil; 
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext; 
    if (managedObjectContext != nil) { 
     if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { 
      // Replace this implementation 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(); 
     } 
    } 
} 



#pragma mark - Core Data stack 

// Returns the managed object context for the application. 
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. 
- (NSManagedObjectContext *)managedObjectContext 
{ 
    if (_managedObjectContext != nil) { 
     return _managedObjectContext; 
    } 

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

// Returns the managed object model for the application. 
// If the model doesn't already exist, it is created from the application's model. 
- (NSManagedObjectModel *)managedObjectModel 
{ 
    if (_managedObjectModel != nil) { 

     NSLog(@"managedObjectModel %@", _managedObjectModel); 

     return _managedObjectModel; 

    } 
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyApp" withExtension:@"momd"]; 
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; 

    NSLog(@"managedObjectModel %@", _managedObjectModel); 


    return _managedObjectModel; 
} 

// Returns the persistent store coordinator for the application. 
// If the coordinator doesn't already exist, it is created and the application's store added to it. 
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (_persistentStoreCoordinator != nil) { 
     return _persistentStoreCoordinator; 
    } 

    //NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TMM.sqlite"]; 
    //NSURL *storeURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSAllDomainsMask] lastObject]; 
// storeURL = [storeURL URLByAppendingPathComponent:@"db.sqlite"]; 

    NSURL *storeURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.ragazzetto.MyApp.Documents"]; 
    storeURL = [storeURL URLByAppendingPathComponent:@"MyApp.sqlite"]; 

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

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

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

    return _persistentStoreCoordinator; 
} 
#pragma mark - Application's Documents directory 

// Returns the URL to the application's Documents directory. 
- (NSURL *)applicationDocumentsDirectory 
{ 
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 
} 


@end 

的內容哪裏有問題 ?

謝謝您的幫助

回答

4

當你寫一個iOS應用extension--包括apps--你要創建一個單獨的可執行文件與自己捆綁所有當前WatchKit。應用中的資源不一定在擴展中可用,反之亦然。所以,當你這樣做:在WatchKit應用程序運行時,比它在你的應用程序包含

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyApp" withExtension:@"momd"]; 
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; 

的URL是不同的。該錯誤告訴您WatchKit應用程序的URL無效,即WatchKit應用程序包中沒有模型文件。

簡單的解決方法是將模型包含在WatchKit包中。這樣做的:

  1. 選擇模型文件在Xcode
  2. 打開文件檢查器面板上的Xcode的窗口
  3. 看右邊「目標成員」部分。確保您的WatchKit目標已被選中。

這應該工作,但意味着你有兩個模型文件的副本。更好的方法是將模型文件放入兩個目標使用的共享框架中。這有點複雜,但有一點搜索會找到詳細的步驟。

+0

謝謝湯姆的幫助! – Ragazzetto