无法从iOS应用

时间:2015-07-06 12:34:19

标签: ios objective-c sqlite core-data

我对iOS开发相对较新,这是我第一次尝试使用CoreData.framework

我用“sqlitebrowser”创建了一个非常简单的数据库,并将其复制到我在XCode中的项目中。我可以看到该文件包含在应用程序包中。我尝试按照以下方法访问AppDelegate类中的数据库:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    //For now using the `SQLite` store generate manually. Make sure to open the
    //store in read only mode since we cannot modify files in the bundle.

    NSURL *storeURL = [[NSBundle mainBundle] URLForResource:@"MatchWatcher" withExtension:@"sqlite"];
    //Should be like below
    NSDictionary *storeOptions = @{NSReadOnlyPersistentStoreOption:@YES, NSSQLitePragmasOption: @{@"journal_mode":@"DELETE"} };
//    NSDictionary *storeOptions = @{NSSQLitePragmasOption: @{@"journal_mode" : @"DELETE"} };
    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if(![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                              configuration:nil
                                                        URL:storeURL
                                                    options:storeOptions
                                                      error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }

    return _persistentStoreCoordinator;
}

正如您所看到的,我尝试使用包含DB的ReadOnly开头的storeOptions变量,但这给了我以下异常:

  

2015-07-06 14:15:18.372 MatchWatcher [27495:3849154] CoreData:错误:-addPersistentStoreWithType:SQLite配置:(null)URL:file:/// Users / john / Library / Developer / CoreSimulator / Devices /F8C9B7DC-3C1D-495C-8FB6-80A9EA5976BB/data/Containers/Bundle/Application/CA64DE15-428B-4356-BB2B-066258E5FBAE/MatchWatcher.app/MatchWatcher.sqlite options:{       NSReadOnlyPersistentStoreOption = 1;       NSSQLitePragmasOption = {           “journal_mode”= DELETE;       };       } ...返回错误错误Domain = NSCocoaErrorDomain Code = 257“操作无法完成。(Cocoa错误257.)”UserInfo = 0x7fa38a73cbb0 {NSUnderlyingException =无法在路径中创建具有只读选项的新数据库文件:/用户/ john / Library / Developer / CoreSimulator / Devices / F8C9B7DC-3C1D-495C-8FB6-80A9EA5976BB / data / Containers / Bundle / Application / CA64DE15-428B-4356-BB2B-066258E5FBAE / MatchWatcher.app / MatchWatcher.sqlite} with userInfo字典{       NSUnderlyingException =“无法在路径中使用只读选项创建新数据库文件:/ Users / john / Library / Developer / CoreSimulator / Devices / F8C9B7DC-3C1D-495C-8FB6-80A9EA5976BB / data / Containers / Bundle / Application / CA64DE15- 428B-4356-BB2B-066258E5FBAE / MatchWatcher.app / MatchWatcher.sqlite“;       }       2015-07-06 14:15:18.373 StockWatcher [27495:3849154]未解决的错误错误Domain = NSCocoaErrorDomain Code = 257“操作无法完成。(Cocoa错误257.)”UserInfo = 0x7fa38a73cbb0 {NSUnderlyingException =无法创建路径上具有只读选项的新数据库文件:/ Users / john / Library / Developer / CoreSimulator / Devices / F8C9B7DC-3C1D-495C-8FB6-80A9EA5976BB / data / Containers / Bundle / Application / CA64DE15-428B-4356-BB2B- 066258E5FBAE / MatchWatcher.app / MatchWatcher.sqlite},{       NSUnderlyingException =“无法在路径中使用只读选项创建新数据库文件:/ Users / john / Library / Developer / CoreSimulator / Devices / F8C9B7DC-3C1D-495C-8FB6-80A9EA5976BB / data / Containers / Bundle / Application / CA64DE15- 428B-4356-BB2B-066258E5FBAE / MatchWatcher.app / MatchWatcher.sqlite“;       }

我做错了什么想法?一些帮助将不胜感激!感谢。

1 个答案:

答案 0 :(得分:1)

Core Data使用的SQLite文件以封闭的方式构建。它并不适用于您在Core Data之外开发的SQLite文件。

核心数据旨在成为您的应用程序的模型。将其作为关系数据库工具处理将导致问题。将其用作应用程序的模型,然后将其保留到磁盘。不是相反。