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

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

最佳答案

得到它的工作。我已经创建了辅助方法来获取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];

关于objective-c - 如何正确初始化RestKit的objectStore以使用Documents以外的目录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11218334/

10-11 04:08