将PersistentStoreCoordinator从本地迁移到iCloud时重复数据

时间:2015-07-17 14:22:31

标签: objective-c core-data ios8 icloud icloud-drive

我正在使用iCloud与核心数据同步工作正常。现在我想添加开/关切换(迁移存储)。如果我将商店迁移到iCloud到本地工作正常,但如果迁移回iCloud数据获得重复。即使我试图在迁移之前删除无处不在的容器没有帮助。我无法进行重复数据删除,因为重复数据后没有调用“StoreDidImportUbiquitousContent”方法。

以下代码:

-(NSURL *)storeURL
{
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Expns.sqlite"];
    return storeURL;
}


    - (void)moveStoreFileToICloud:(NSURL*)fileURL 
    {
     NSPersistentStoreCoordinator * persistentStoreCoordinator = self.persistentStoreCoordinator;

            id sourceStore = [[persistentStoreCoordinator persistentStores] firstObject];

            if (!sourceStore) {

                NSLog(@" failed to add old store");

            } else {
                NSLog(@" Successfully added store to migrate");

                bool moveSuccess = NO;
                NSError *error;


                NSLog(@" About to migrate the store...");
                // Now migrate the store using the iCloud options
                id migrationSuccess = [self.persistentStoreCoordinator migratePersistentStore:sourceStore toURL:[self storeURL] options:[self icloudStoreOptions] withType:NSSQLiteStoreType error:&error];

                if (migrationSuccess) {
                    moveSuccess = YES;
                    NSLog(@"store successfully migrated");

                    // Now delete the local file
                    if (shouldDelete) {
                        NSLog(@" deleting local store");
                        [self deleteLocalStore];
                    } else {
                        NSLog(@" not deleting local store");
                    }
                }
                else {
                    NSLog(@"Failed to migrate store: %@, %@", error, error.userInfo);
                }

            }
    }

以上代码正常工作。

//迁移回iCloud

// fileURL是本地文件网址

#include <fstream>
#include "file_to_string.h"

std::string file_to_(const char* name)
{
    std::ifstream in(name);
    std::string output((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
    return output;
}

现在,数据会在迁移之前复制。我试图删除无处不在的容器也没有帮助。

1 个答案:

答案 0 :(得分:0)

我通过像this

手动重复删除我的数据库来解决这个问题