2012-02-11 54 views
2

我試圖做一些非常類似於正在做的事情here,但由於沒有給出真正的答案,我想看看是否有人可以幫助我與我特別的問題。NSManagedObjectContext = [NSEntityDescription ...]後的SIGABRT

我只是試圖添加核心數據到我有的現有應用程序。 下面是我已經添加到相應的文件。 我也概述了我的應用程序加載時我在哪裏得到SIGABRT。 我已驗證「loadData」中的上下文變量不爲NULL。

AppDelegate.h

#import <UIKit/UIKit.h> 
#import <CoreData/CoreData.h> 
@interface AppDelegate : NSObject <UIApplicationDelegate> { 
} 

... 

@property (readonly, retain, nonatomic) NSManagedObjectContext *managedObjectContext; 
@property (readonly, retain, nonatomic) NSManagedObjectModel *managedObjectModel; 
@property (readonly, retain, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;  
-(void)loadData; 
@end 

AppDelegate.m

#import "NewModel.h" 

@synthesize managedObjectContext = __managedObjectContext; 
@synthesize managedObjectModel = __managedObjectModel; 
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator; 
... 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    ... 

    __managedObjectContext = [[NSManagedObjectContext alloc] init]; 
    __managedObjectModel = [[NSManagedObjectModel alloc] init]; 
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] init]; 
    [__managedObjectContext setPersistentStoreCoordinator:__persistentStoreCoordinator];   
    [self loadData]; 
} 

- (NSManagedObjectContext *)managedObjectContext { 
    if (__managedObjectContext) { 
     return __managedObjectContext; 
     } 

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


-(void)loadData 
{ 
    NSManagedObjectContext *context = [self managedObjectContext]; 

    NSManagedObjectModel *newObj; // Tried NewModel = *newObj; thinking that may resolve 
            //   the issue, didn't work though 
    newObj = [NSEntityDescription 
       insertNewObjectForEntityForName:@"NewModel" 
       inManagedObjectContext:context]; --> SIGABRT WHEN TRYING TO EXECUTE THIS 
    [newTeam setValue:@"value" forKey:@"modelValue"]; 
    NSError *error; 
    [context save:&error]; 
} 

NewModel.h

#import <Foundation/Foundation.h> 
#import <CoreData/CoreData.h> 

@interface NewModel : NSManagedObject  
@property (nonatomic, retain) NSString * modelvalue; 
@end 

NewModel.m

#import "NewModel.h" 

@implementation NewModel 
@dynamic modelValue; 
@end 

回答

0

哪一個,newmodel或teammodel?

@interface NewModel : NSManagedObject  
@implementation TeamModel 
+0

抱歉,這是在我的代碼的新模式,在這個問題犯了一個錯誤,我會解決這個問題 – Vince613 2012-02-11 19:09:35

0

你寫這個getter方法:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    return __persistentStoreCoordinator; 
} 

你把它在這一行,但我沒有看到它的任何地方定義。

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 
if (!coordinator) { 
    return nil; 
} 
+0

不,我沒有,感謝您的漁獲物,但它並沒有解決我的問題。仍然命中相同SIGABRT – Vince613 2012-02-11 19:14:18

0

有您所創建的數據模型,啓動應用程序,然後更改的數據模型,然後嘗試再次啓動應用程序?如果是這樣,您的應用將崩潰,因爲持久性存儲與數據模型不兼容。您需要刪除您的應用並重新安裝,而不是安裝在「頂部」。

另外...請把一個斷點,並確保'上下文'不是零。我假設你有@synthesize managedObjectContext = __managedObjectContext; ??

+0

因此,當我嘗試向我的NewModel實體中插入新的託管對象時,應用程序崩潰。它從來沒有加載超過這一點。我試過刪除應用程序並重新構建並運行它,但沒有得到任何不同的結果。 而且我確認__managedObjectContext不是零。我有@synthesize代碼並將其添加到上面。 – Vince613 2012-02-11 21:19:18

1

我上面的代碼的問題是,我沒有正確地正確初始化我的persistentStoreCoordinator或managedObjectModel。以下是一次添加的兩個功能,讓我知道這個問題。

/** 
    Returns the managed object model for the application. 
    If the model doesn't already exist, it is created by merging all of the models found in the application bundle. 
    */ 

- (NSManagedObjectModel *)managedObjectModel { 

    if (managedObjectModel != nil) { 
     return managedObjectModel; 
    } 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"LocalDatabase" ofType:@"momd"]; 
    NSURL *momURL = [NSURL fileURLWithPath:path]; 
    managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL]; 

    //managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];  
    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; 
     } 

     NSString *storePath = [ [self applicationDocumentsDirectory] stringByAppendingPathComponent:@"LocalDatabase.db"]; 
     NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; 

     // Put down default db if it doesn't already exist 
     NSFileManager *fileManager = [NSFileManager defaultManager]; 
     if (![fileManager fileExistsAtPath:storePath]) { 
      NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"LocalDatabase" ofType:@"sqlite"]; 
      if (defaultStorePath) { 
       [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL]; 
      } 
     } 

     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; 
    } 
相關問題