【问题标题】:MagicalRecord: Remove entire data and setup core data stack againMagicalRecord:删除整个数据并再次设置核心数据堆栈
【发布时间】:2012-09-10 00:31:37
【问题描述】:

对于每次应用更新,我想彻底擦除我的整个 Core Data 数据库,然后重新设置它。我没有成功这样做。我尝试了各种事情,这似乎是我最接近的。我找到了几个 SO 帖子,但没有一个解决方案适合我的目的。

我正在使用MagicalRecord,它提供了几种获取各种对象的简写方法。这是我的代码:

if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"buildVersion"] intValue] < [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] intValue]) {
    NSPersistentStore *store = [NSPersistentStore defaultPersistentStore];
    NSError *error;
    NSURL *storeURL = [NSPersistentStore defaultLocalStoreUrl];
    NSPersistentStoreCoordinator *storeCoordinator = [NSPersistentStoreCoordinator defaultStoreCoordinator];
    [storeCoordinator removePersistentStore:store error:&error];
    [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];
    [[NSUserDefaults standardUserDefaults] setObject:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] forKey:@"buildVersion"];
}

[MagicalRecord setupCoreDataStack];

【问题讨论】:

    标签: ios core-data magicalrecord


    【解决方案1】:

    有一个NSPersistentStore+MagicalRecord 类别允许您获取持久存储的 URL:

    + (NSURL *) MR_urlForStoreName:(NSString *)storeFileName;
    

    您可以使用此调用中的 URL 来使用 NSFileManager 删除持久存储 SQLite db。

    【讨论】:

      猜你喜欢
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多