0

所以我看到 RestKit 的参考中有一个 init 方法:objectStoreWithStoreFilename:inDirectory:usingSeedDatabaseName:managedObjectModel:delegate假设在指定的目录中初始化 objectStore。“inDirectory”参数接受 NSString。我已经尝试过@"Library/Caches" 和 [self.applicationCachesDirectory absoluteURL],其中 self.applicationCachesDirectory 返回 Caches 目录的 URL,但我遇到了异常。

请告诉我如何在此方法中指定“库/缓存”以便将 sqlite DB 保存在缓存目录而不是文档中?

4

1 回答 1

1

得到它的工作。我创建了辅助方法来获取 Caches 文件夹路径作为 NSString

    - (NSString *)applicationCachesDirectoryString
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [paths objectAtIndex:0];
    BOOL isDir = NO;
    NSError *error;
    if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO) {
        [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
    }
    return cachePath;
}

然后将其传递给 objectStore init 方法:

self.objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:databaseName inDirectory:[self applicationCachesDirectoryString] usingSeedDatabaseName:seedDatabaseName managedObjectModel:managedObjectModel delegate:self];
于 2012-06-27T02:14:11.413 回答