2012-02-26 54 views
-1

只是想知道是否有其他人遇到过这种情况。coredata无法在xcode 4.3上工作?

我得到了这段代码,曾经在以前的xcode版本中工作辉煌。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

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

    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"mydb.sqlite"]; 
    /* 
    Set up the store. 
    For the sake of illustration, provide a pre-populated default store. 
    */ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    // If the expected store doesn't exist, copy the default store. 
    if (![fileManager fileExistsAtPath:storePath]) { 
     NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"mydb" ofType:@"sqlite"]; 
     if (defaultStorePath) { 
      [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL]; 
     } 
    } 

    NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; 

    [self addSkipBackupAttributeToItemAtURL:storeUrl]; 

    NSError *error; 
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]]; 
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 

     Typical reasons for an error here include: 
     * The persistent store is not accessible 
     * The schema for the persistent store is incompatible with current managed object model 
     Check the error message to determine what the actual problem was. 
     */ 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    }  

    return persistentStoreCoordinator; 
} 

我期望从这一如下:

  1. 一个空的“mydb的”从头开始创建,如果在我的包没有“mydb.sqlite”。
  2. 如果一个“mydb.sqlite”存在于我的主包中,那么我会期望它被复制到指定的diretory中。
  3. 如果“mydb.sqlite”与我的xcdatamodel不兼容,则应用程序必须崩溃。

但是,这只适用于以前已经创建的数据库。 例如,如果我尝试把一个名为“mydb.sqlite”的随机数据库放到我的包中,然后删除原来的一个,那么,

该应用程序不会崩溃!一个空白数据库被创建并且新的数据库被忽略。 这是完全错误的,因为它违背了我的代码。

此外,如果我加回原来的数据库没有任何反应,应用程序只是创建一个空白数据库。

(是的,我做我的清理项目,删除SIM卡的应用程序,并且发生任何改变之前甚至删除build文件夹!)

任何想法?

+0

你的错误是什么?你可以把它发布出来吗? – Raptor 2012-02-26 10:26:46

+0

没有错误。这只是观察不能按照我的代码 – 2012-02-26 10:28:16

回答

0

问题/观察的答案很简单。

降级到4.2.1

我已经恢复了我的Xcode 4.2.1(感谢上帝的Time Machine),现在一切都如预期。

今天晚些时候我会向苹果提交一个错误报告。

0

您的代码与您的期望有所不同,正如您所说的那样。

这是您的期望:

  1. 一个空的“mydb的”从头开始建立,如果没有“mydb.sqlite”在我的包。
  2. 如果在我的主包中存在“mydb.sqlite”,那么我希望它被复制到指定的目录中。
  3. 如果“mydb.sqlite”与我的xcdatamodel不兼容,则应用程序必须崩溃。

这里是你的代码做什么:

  1. 寻找在Documents目录
  2. 如果mydb.sqlite不存在mydb.sqlite,复制它从主束(如果它存在于主束)
  3. 创建一个新的持久性存储在〜/文档/ mydb.sqlite

我觉得您遇到的主要问题是由于第3步:addPersistentStoreWithType:...在提供的URL处创建新的商店。您需要重新查询文件是否存在(以检查是否存在可能已从MainBundle复制的文件),如果存在,则使用[persistentStoreCoordinator persistentStoreForURL:storeURL]而不是创建新的持久性存储。

一旦你解决了这个问题,其他问题应该更容易追踪。我建议在你的代码中增加更多的NSLog,这样你就可以看到代码是否遵循你期望的执行路径。例如。记录您创建的每个新对象,以便您可以轻松查看它们中的任何一个是否为nil。当你复制你的包数据库时,添加一个NSError指针而不是NULL,然后如果错误不是零,请记录它。

+0

应该是这样的方式对不起。你说什么,我期望什么。我没有在我的问题中说清楚。然而,这是DOESNT的工作。 – 2012-02-26 17:51:41

0

检查您的默认数据库实际上是否包含在软件包中 - 即它被检查为包含在项目中,并处于复制文件构建阶段。我重新安排了一个项目和一个SQLite文件,同时将其复制到项目结构中,但未包含在我的项目中 - 这会导致您看到的行为。