zoukankan      html  css  js  c++  java
  • Core Data 中遇到的一些问题

    persistentStoreCoordinator
     1 - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
     2 {
     3     if (__persistentStoreCoordinator != nil) {
     4         return __persistentStoreCoordinator;
     5     }
     6     
     7     NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Locations.sqlite"];
     8     
     9     NSError *error = nil;
    10     __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    11     if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
    12 //        [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];
    13         /*
    14          Replace this implementation with code to handle the error appropriately.
    15          
    16          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. 
    17          
    18          Typical reasons for an error here include:
    19          * The persistent store is not accessible;
    20          * The schema for the persistent store is incompatible with current managed object model.
    21          Check the error message to determine what the actual problem was.
    22          
    23          
    24          If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
    25          
    26          If you encounter schema incompatibility errors during development, you can reduce their frequency by:
    27          * Simply deleting the existing store:
    28          [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
    29          
    30          * Performing automatic lightweight migration by passing the following dictionary as the options parameter: 
    31          [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
    32          
    33          Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
    34          
    35          */
    36         NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    37         abort();
    38     }    
    39     
    40     return __persistentStoreCoordinator;
    41 }

    方法中遇到错误(

    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]){}语句处

    ):

    Error
     1 2012-08-29 22:30:15.288 Locations[942:fb03] Unresolved error Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x6b6a630 {metadata=<CFBasicHash 0x6b6ba40 [0x182db48]>{type = immutable dict, count = 7,
     2 entries =>
     3     2 : <CFString 0x6b6e210 [0x182db48]>{contents = "NSStoreModelVersionIdentifiers"} = <CFArray 0x6b6aee0 [0x182db48]>{type = immutable, count = 1, values = (
     4     0 : <CFString 0x1828cd8 [0x182db48]>{contents = ""}
     5 )}
     6     4 : <CFString 0x6b61200 [0x182db48]>{contents = "NSPersistenceFrameworkVersion"} = <CFNumber 0x6b6d730 [0x182db48]>{value = +386, type = kCFNumberSInt64Type}
     7     6 : <CFString 0x6b68410 [0x182db48]>{contents = "NSStoreModelVersionHashes"} = <CFBasicHash 0x6b679e0 [0x182db48]>{type = immutable dict, count = 0,
     8 entries =>
     9 }
    10 
    11     7 : <CFString 0x1183ad8 [0x182db48]>{contents = "NSStoreUUID"} = <CFString 0x6b6e340 [0x182db48]>{contents = "D15F4513-8CE5-4CD4-91C2-8C697819F5D4"}
    12     8 : <CFString 0x1183978 [0x182db48]>{contents = "NSStoreType"} = <CFString 0x1183988 [0x182db48]>{contents = "SQLite"}
    13     9 : <CFString 0x6b67a30 [0x182db48]>{contents = "_NSAutoVacuumLevel"} = <CFString 0x6b32180 [0x182db48]>{contents = "2"}
    14     10 : <CFString 0x6b6b780 [0x182db48]>{contents = "NSStoreModelVersionHashesVersion"} = <CFNumber 0x6b30e60 [0x182db48]>{value = +3, type = kCFNumberSInt32Type}
    15 }
    16 , reason=The model used to open the store is incompatible with the one used to create the store}, {
    17     metadata =     {
    18         NSPersistenceFrameworkVersion = 386;
    19         NSStoreModelVersionHashes =         {
    20         };
    21         NSStoreModelVersionHashesVersion = 3;
    22         NSStoreModelVersionIdentifiers =         (
    23             ""
    24         );
    25         NSStoreType = SQLite;
    26         NSStoreUUID = "D15F4513-8CE5-4CD4-91C2-8C697819F5D4";
    27         "_NSAutoVacuumLevel" = 2;
    28     };
    29     reason = "The model used to open the store is incompatible with the one used to create the store";
    30 }
    reason = "The model used to open the store is incompatible with the one used to create the store";

    经过查看错误信息:用来打开存储的模型和用来创建存储的模型不兼容。

    通过- (NSPersistentStoreCoordinator *)persistentStoreCoordinator方法中的注释:

    If you encounter schema incompatibility errors during development, you can reduce their frequency by:
             * Simply deleting the existing store:
             [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
    
    .....

    找到解决方案。

  • 相关阅读:
    【MVC整理】1.使用 StructureMap 作为 ASP.NET MVC 的 DI 框架
    一个随机列表项算法
    轻量级IOC框架:Ninject
    解决“找不到请求的 .Net Framework 数据提供程序。可能没有安装”的问题
    Vcastr 2.2 flv 网络播放器 参数设置
    C#取汉字首字母
    IIS Express的安装与设置讲解
    清晰的图片缩略方案
    ssh远程连接ubuntu
    【Apache】在Apache中利用ServerAlias设置虚拟主机接收多个域名和设置域名泛解析
  • 原文地址:https://www.cnblogs.com/submarinex/p/2662850.html
Copyright © 2011-2022 走看看