gpt4 book ai didi

ios - RESTKit:RKManagedObjectStore & 删除数据

转载 作者:行者123 更新时间:2023-11-29 12:47:39 25 4
gpt4 key购买 nike

下面是我必须设置 ObjectStore 的代码,它工作正常。

- (RKManagedObjectStore *)setupCoreDataWithRESTKit{
NSError * error;
NSURL * modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"app" ofType:@"momd"]];
NSManagedObjectModel * managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
self.managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

[self.managedObjectStore createPersistentStoreCoordinator];

NSArray * searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentPath = [searchPaths objectAtIndex:0];
NSPersistentStore * persistentStore = [self.managedObjectStore addSQLitePersistentStoreAtPath:[NSString stringWithFormat:@"%@/app.sqlite", documentPath] fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

if(!persistentStore){
NSLog(@"Failed to add persistent store: %@", error);
}

[self.managedObjectStore createManagedObjectContexts];
return self.managedObjectStore;
}

- (id)optionsForSqliteStore {
return @{
NSInferMappingModelAutomaticallyOption: @YES,
NSMigratePersistentStoresAutomaticallyOption: @YES
};
}

问题 1:我需要多久调用一次这段代码?首次启动应用程序时仅一次?还是每次启动应用程序?或者当我需要访问商店时每次发布仅一次?

问题 2:我缓存每个用户的数据。如果用户退出应用程序,我想删除所有数据/对象(或数据存储区?),关于执行此操作的最佳做​​法有什么建议吗?

最佳答案

How often do I need to call this code?

每次启动应用程序

I cache the data per User. If the User logs out of the app, I would like to delete all of the data/objects (or the datastore?), any suggestion on what's the best practice to do this?

如果要删除所有内容,则销毁托管对象上下文和存储协调器,并从磁盘中删除 SQLite 文件。

如果你只想删除一些东西,那么从上下文中删除托管对象并保存上下文(和持久存储)。

关于ios - RESTKit:RKManagedObjectStore & 删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23246271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com