Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 目标C:将本地核心数据存储移动到icloud,反之亦然_Ios_Objective C_Core Data_Icloud - Fatal编程技术网

Ios 目标C:将本地核心数据存储移动到icloud,反之亦然

Ios 目标C:将本地核心数据存储移动到icloud,反之亦然,ios,objective-c,core-data,icloud,Ios,Objective C,Core Data,Icloud,我在这方面遇到了一些问题,现在读了几本教程和一本书中的例子,但是这本书建议了一个更深入的副本,这对我来说太复杂了。不管怎么说,iCloud的东西让人困惑。我做对了吗: 在应用程序中启用iCloud的用户: -尝试将本地存储复制到icloud -删除本地存储 用户在应用程序中禁用iCloud: -尝试将存储复制到本地 -删除无处不在的内容 -删除icloud的本地副本 我错过什么了吗 额外问题:如何在复制过程中“阻止”应用程序,以及应将此“阻止”代码放置在何处 这将是我的代码: - (bool)m

我在这方面遇到了一些问题,现在读了几本教程和一本书中的例子,但是这本书建议了一个更深入的副本,这对我来说太复杂了。不管怎么说,iCloud的东西让人困惑。我做对了吗:

在应用程序中启用iCloud的用户: -尝试将本地存储复制到icloud -删除本地存储

用户在应用程序中禁用iCloud: -尝试将存储复制到本地 -删除无处不在的内容 -删除icloud的本地副本

我错过什么了吗

额外问题:如何在复制过程中“阻止”应用程序,以及应将此“阻止”代码放置在何处

这将是我的代码:

- (bool)moveStoreToICloud {
    NSLog(<#NSString * _Nonnull format, ...#>)(@" called");
    return [self moveStoreFileToICloud:self.store delete:YES backup:YES];
}

- (bool)moveStoreFileToICloud:(NSURL*)fileURL delete:(bool)shouldDelete backup:(bool)shouldBackup {
    NSLog(@" called");

    // Always make a backup of the local store before migrating to iCloud
    if (shouldBackup)
        [self backupLocalStore];

    NSPersistentStoreCoordinator *migrationPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.model];

    // Open the existing local store using the original options
    NSDictionary *options =
    @{
      NSMigratePersistentStoresAutomaticallyOption:@YES
      ,NSInferMappingModelAutomaticallyOption:@YES
      ,NSPersistentStoreUbiquitousContentNameKey:ubiquitousContentNameKey
      //,NSPersistentStoreUbiquitousContentURLKey:@"ChangeLogs" // Optional since iOS7
      };
    id sourceStore = [migrationPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:fileURL options:options error:nil];

    if (!sourceStore) {
        NSLog(@" failed to add old store");
        return FALSE;
    } 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 = [migrationPSC migratePersistentStore:sourceStore toURL:[_iCloudStore URL] options:options withType:NSSQLiteStoreType error:&error];

        if (migrationSuccess) {
            moveSuccess = YES;
            NSLog(@"store successfully migrated");
            // Now delete the local file
            if (shouldDelete) {
                NSLog(@" deleting local store");
                [self destroyAllLocalDataForThisApplication];
            } else {
                NSLog(@" not deleting local store");
            }

            [self resetCoreData];
            [self setupCoreData];

            [[NSNotificationCenter defaultCenter] postNotificationName:@"SomethingChanged"
                                                                object:nil];
            return TRUE;
        }
        else {
            NSLog(@"Failed to migrate store: %@, %@", error, error.userInfo);
            return FALSE;
        }

    }
    return FALSE;
}
/*! Moves an iCloud store to local by migrating the iCloud store to a new local store and then removes the store from iCloud.

 Note that even if it fails to remove the iCloud files it deletes the local copy.  User may need to clean up orphaned iCloud files using a Mac!

 @return Returns YES of file was migrated or NO if not.
 */
- (bool)moveStoreToLocal {
    NSLog(@"moveStoreToLocal called");

    // Lets use the existing PSC
    NSPersistentStoreCoordinator *migrationPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.model];

    // Open the store
    NSDictionary *options =
    @{
      NSMigratePersistentStoresAutomaticallyOption:@YES
      ,NSInferMappingModelAutomaticallyOption:@YES
      ,NSPersistentStoreUbiquitousContentNameKey:ubiquitousContentNameKey
      //,NSPersistentStoreUbiquitousContentURLKey:@"ChangeLogs" // Optional since iOS7
      };
    id sourceStore = [migrationPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[_iCloudStore URL] options:options error:nil];

    if (!sourceStore) {

        NSLog(@" failed to add old store");
        return FALSE;
    } else {
        NSLog(@" Successfully added store to migrate");

        bool moveSuccess = NO;
        NSError *error;

        NSLog(@" About to migrate the store...");
        id migrationSuccess = [migrationPSC migratePersistentStore:sourceStore toURL:[_store URL] options:options withType:NSSQLiteStoreType error:&error];

        if (migrationSuccess) {
            moveSuccess = YES;
            NSLog(@"store successfully migrated");
            // Now delete the local file
            [self destroyAlliCloudDataForThisApplication];

            [self resetCoreData];
            [self setupCoreData];

            [[NSNotificationCenter defaultCenter] postNotificationName:@"SomethingChanged"
                                                                object:nil];
            return TRUE;
        }
        else {
            NSLog(@"Failed to migrate store: %@, %@", error, error.userInfo);
            return FALSE;
        }

    }

    return TRUE;
}


#pragma mark - ICLOUD RESET
- (void)destroyAllLocalDataForThisApplication {

    if (![[NSFileManager defaultManager] fileExistsAtPath:[[_store URL] path]]) {
        NSLog(@"Skipped destroying content, _store.URL is %@",[[_store URL] path]);
        return;
    }

    NSLog(@"\n\n\n\n\n **** Destroying ALL local content for this application, this could take a while...  **** \n\n\n\n\n\n");

    [self removeAllStoresFromCoordinator:_coordinator];
    [self removeAllStoresFromCoordinator:_seedCoordinator];
    _coordinator = nil;
    _seedCoordinator = nil;

    NSError *error;
    if([[NSFileManager defaultManager] removeItemAtURL:_storeURL error:&error]){
        NSLog(@"Local store successfully removed");
    } else {
        NSLog(@"\n\n **** FAILED to destroy this application's iCloud content at URL (%@) **** \n%@\n",[_store URL],error);
    }
}


- (void)destroyAlliCloudDataForThisApplication {

    if (![[NSFileManager defaultManager] fileExistsAtPath:[[_iCloudStore URL] path]]) {
        NSLog(@"Skipped destroying iCloud content, _iCloudStore.URL is %@",[[_iCloudStore URL] path]);
        return;
    }

    NSLog(@"\n\n\n\n\n **** Destroying ALL iCloud content for this application, this could take a while...  **** \n\n\n\n\n\n");

    [self removeAllStoresFromCoordinator:_coordinator];
    [self removeAllStoresFromCoordinator:_seedCoordinator];
    _coordinator = nil;
    _seedCoordinator = nil;

    NSDictionary *options =
    @{
      NSPersistentStoreUbiquitousContentNameKey:ubiquitousContentNameKey
      //,NSPersistentStoreUbiquitousContentURLKey:@"ChangeLogs" // Optional since iOS7
      };
    NSError *error;
    if ([NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:[_iCloudStore URL]
                                                                             options:options
                                                                               error:&error]) {
        NSLog(@"\n\n\n\n\n");
        NSLog(@"*        This application's iCloud content has been destroyed        *");
        NSLog(@"* On ALL devices, please delete any reference to this application in *");
        NSLog(@"*  Settings > iCloud > Storage & Backup > Manage Storage > Show All  *");
        NSLog(@"\n\n\n\n\n");
        [[NSFileManager defaultManager] removeItemAtURL:[_iCloudStore URL] error:&error]
    } else {
        NSLog(@"\n\n **** FAILED to destroy this application's iCloud content at URL (%@) **** \n%@\n",[_iCloudStore URL],error);
    }
}
-(bool)移动存储到云{
NSLog()(@“调用”);
返回[self-moveStoreFileToICloud:self.store删除:是备份:是];
}
-(bool)moveStoreFileToICloud:(NSURL*)文件URL删除:(bool)应删除备份:(bool)应备份{
NSLog(@“调用”);
//在迁移到iCloud之前,请始终备份本地存储
如果(应该备份)
[自备份存储];
NSPersistentStoreCoordinator*migrationPSC=[[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:self.model];
//使用原始选项打开现有的本地存储
NSDictionary*选项=
@{
NSMigratePersistentStoresAutomaticallyOption:@是
,nsinFermappingModelAutomatically选项:@是
,NSPersistentStoreUbiquitousContentNameKey:ubiquitousContentNameKey
//,nsPersistentStoreUbiquityousContentUrlKey:@“ChangeLogs”//iOS7之后的可选项
};
id sourceStore=[migrationPSC addPersistentStoreWithType:NSSQLiteStoreType配置:nil URL:fileURL选项:选项错误:nil];
if(!sourceStore){
NSLog(@“未能添加旧存储”);
返回FALSE;
}否则{
NSLog(@“已成功添加要迁移的存储”);
bool-moveSuccess=否;
n错误*错误;
NSLog(@“即将迁移存储…”);
//现在使用iCloud选项迁移存储
id migrationSuccess=[migrationPSC migratePersistentStore:sourceStore toURL:[\u iCloudStore URL]选项:类型为NSSQLiteStoreType的选项错误:&错误];
如果(迁移成功){
moveSuccess=是;
NSLog(@“存储已成功迁移”);
//现在删除本地文件
如果(应删除){
NSLog(@“删除本地存储”);
[self-DestroyAllLocalDataforThis Application];
}否则{
NSLog(@“不删除本地存储”);
}
[自复位CoreData];
[自设置CoreData];
[[NSNotificationCenter defaultCenter]postNotificationName:@“SomethingChanged”
对象:无];
返回TRUE;
}
否则{
NSLog(@“未能迁移存储:%@,%@”,错误,错误.userInfo);
返回FALSE;
}
}
返回FALSE;
}
/*! 通过将iCloud存储迁移到新的本地存储,然后从iCloud中删除该存储,将iCloud存储移动到本地。
请注意,即使无法删除iCloud文件,也会删除本地副本。用户可能需要使用Mac清理孤立的iCloud文件!
@返回文件已迁移的是,如果不是,则返回否。
*/
-(bool)移动存储到本地{
NSLog(@“moveStoreToLocal调用”);
//让我们使用现有的PSC
NSPersistentStoreCoordinator*migrationPSC=[[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:self.model];
//开店
NSDictionary*选项=
@{
NSMigratePersistentStoresAutomaticallyOption:@是
,nsinFermappingModelAutomatically选项:@是
,NSPersistentStoreUbiquitousContentNameKey:ubiquitousContentNameKey
//,nsPersistentStoreUbiquityousContentUrlKey:@“ChangeLogs”//iOS7之后的可选项
};
id sourceStore=[migrationPSC addPersistentStoreWithType:NSSQLiteStoreType配置:nil URL:[\u iCloudStore URL]选项:选项错误:nil];
if(!sourceStore){
NSLog(@“未能添加旧存储”);
返回FALSE;
}否则{
NSLog(@“已成功添加要迁移的存储”);
bool-moveSuccess=否;
n错误*错误;
NSLog(@“即将迁移存储…”);
id migrationSuccess=[migrationPSC migratePersistentStore:sourceStore toURL:[\u store URL]选项:带有类型的选项:NSSQLiteStoreType错误:&error];
如果(迁移成功){
moveSuccess=是;
NSLog(@“存储已成功迁移”);
//现在删除本地文件
[该应用程序的自销毁云数据];
[自复位CoreData];
[自设置CoreData];
[[NSNotificationCenter defaultCenter]postNotificationName:@“SomethingChanged”
对象:无];
返回TRUE;
}
否则{
NSLog(@“未能迁移存储:%@,%@”,错误,错误.userInfo);
返回FALSE;
}
}
返回TRUE;
}
#pragma标记-ICLOUD重置
-(void)销毁此应用程序的所有本地数据{
如果(![[NSFileManager defaultManager]文件存在路径:[[u存储URL]路径]]){
NSLog(@“跳过销毁内容,[u store.URL为%@”,[[u store URL]路径]);
返回;
}
NSLog(@“\n\n\n\n****正在销毁此应用程序的所有本地内容,这可能需要一段时间…******\n\n\n\n”);
[self-removeAllStoresFromCoordinator:_coordinator];
[self-removeAllStoresFromCoordinator:_-seedCoordinator];
_协调者=零;
_seedCoordinator=nil;
n错误*错误;
如果([[NSFileManager defaultManager]RemoveItemAttribute:\u存储URL错误:&错误]){
NSLog(@“本地存储已成功删除”);
}否则{
NSLog(@“\n\n****未能在URL(%@)****\n%@\n”处销毁此应用程序的iCloud内容,[\u存储URL],错误);
}
}
-(空)云