如何正确初始化RestKit的objectStore以使用Documents以外的目录

时间:2012-06-27 01:51:07

标签: objective-c ios rest core-data restkit

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

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

1 个答案:

答案 0 :(得分: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];