我在我的应用程序中使用核心数据,我有4-5个表,其中一个是userProfile表。我已经在应用程序中实现了注销。如果用户登录出应用程序im,删除用户配置文件,并将插入新的,如果登录到其他用户帐户。我想删除所有的记录,从数据库中的用户档案删除。im为此使用关系,但即使用户配置文件记录已被删除,也不会从db中删除其他记录。我想提到的一件事是,所有的数据都来自服务部门。我正在使用级联删除规则,用于在userprofile表和其他表之间创建关系。
发布于 2014-03-12 13:39:09
你有两种不同的方法来实现这一点。
第一种方法是删除存储并再次创建它。例如,这意味着访问文件系统中的存储并删除sql文件。例如,您可以在以下讨论中找到如何实现它:删除/重置核心数据中的所有条目?。
第二个解决方案是在UserProfile
实体中创建一个级联关系,将其他实体连接起来。在后一种情况下,您必须建立一个逆关系( would将是正确的方法)。有关更多信息,请参见我在在核心数据中建立父子关系的回答。
这样说,根据我的经验,我不鼓励在Core数据中保存用户信息(例如密码)。相反,采用密钥链来实现这一点。有些库可以简单地封装密钥链访问(例如SSKeychain)。
发布于 2014-03-25 09:46:42
我试过使用级联规则来使用关系,但没有对我起作用,所以我使用了删除/重置核心数据中的所有条目方法。用于此操作的代码。
NSError * error;
NSURL * storeURL = [[AppDelegate.managedObjectContext persistentStoreCoordinator] URLForPersistentStore:[[[AppDelegate.managedObjectContext persistentStoreCoordinator] persistentStores] lastObject]];
[AppDelegate.managedObjectContext lock];
[AppDelegate.managedObjectContext reset];//to drop pending changes
//delete the store from the current managedObjectContext
if ([[AppDelegate.managedObjectContext persistentStoreCoordinator] removePersistentStore: [[[AppDelegate.managedObjectContext persistentStoreCoordinator] persistentStores] lastObject] error:&error])
{
// remove the file containing the data
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error];
//recreate the store like in the appDelegate method
[[[AppDelegate managedObjectContext] persistentStoreCoordinator] addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options: @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} error:&error];
}
[AppDelegate.managedObjectContext unlock];
//that's it !
这对我有用。
https://stackoverflow.com/questions/22351106
复制相似问题