简体   繁体   English

如何强制iCloud与核心数据进行同步?

[英]How to force iCloud with core data to do sync?

I'm using iCloud with core data to sync data of my App. 我正在使用带有核心数据的iCloud来同步我的应用程序的数据。

I tried everything and finally It works perfectly. 我尝试了一切,最后它完美无缺。

but I have one question. 但我有一个问题。

Is there any way that saying to iCloud to sync? 有没有办法说iCloud同步?

It sync when app begin, but the other time. 它在app开始时同步,但另一次。 It seems like that it sync randomly. 它似乎随机同步。 I can't find the way to handle it myself. 我找不到自己处理它的方法。

any help? 任何帮助?

Thanks. 谢谢。

I figured out how to force a core data store to sync to iCloud. 我想出了如何强制核心数据存储同步到iCloud。 You just need to "touch" any receipt files in the Data folder in the Ubiquity filesystem. 您只需要“触摸”Ubiquity文件系统中Data文件夹中的任何收据文件。 The code below will do this. 下面的代码将执行此操作。 To force a sync, call the syncCloud method. 要强制同步,请调用syncCloud方法。 For every "receipt" file it finds, it will update the files modification time stamp to the current time, essentially doing a "touch" on the file. 对于它找到的每个“收据”文件,它会将文件修改时间戳更新为当前时间,基本上对文件进行“触摸”。 As soon as this happens, iCloud will check and sync any core data files that need synchronization in iCloud. 一旦发生这种情况,iCloud将检查并同步任何需要在iCloud中同步的核心数据文件。 Near as I can tell, this is how the simulator or Xcode does it when you select the "Trigger iCloud Sync" option. 我可以说,这就是当你选择“Trigger iCloud Sync”选项时模拟器或Xcode如何做到这一点。

@property (strong, nonatomic) NSMetadataQuery  *query;

-(void) init
{
    self.query = [[NSMetadataQuery alloc] init];

    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(didFinishMetadataQuery)
                                                 name: NSMetadataQueryDidFinishGatheringNotification
                                               object: self.query];
}

-(void) didFinishMetadataQuery
{
    // Query completed so mark it as completed
    [self.query stopQuery];

    // For each receipt, set the modification date to now to force an iCloud sync
    for(NSMetadataItem *item in self.query.results)
    {
        [[NSFileManager defaultManager] setAttributes: @{NSFileModificationDate:[NSDate date]}
                                         ofItemAtPath: [[item valueForAttribute: NSMetadataItemURLKey] path]
                                                error: nil];
    }
}

-(void) syncCloud
{
    // Look for files in the ubiquity container data folders
    [self.query setSearchScopes:@[NSMetadataQueryUbiquitousDataScope]];

    // Look for any files named "receipt*"
    [self.query setPredicate:[NSPredicate predicateWithFormat:@"%K like 'receipt*'", NSMetadataItemFSNameKey]];

    // Start the query on the main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        BOOL startedQuery = [self.query startQuery];
        if (!startedQuery)
        {
            NSLog(@"Failed to start query.\n");
            return;
        }
    });
}

You have to force any NSURL item to be cached in iCloud with this method: 您必须使用此方法强制在iCloud中缓存任何NSURL项:

- (BOOL)addBackupAttributeToItemAtURL:(NSURL *)URL
{
    NSLog(@"URL: %@", URL);
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);

    NSError *error = nil;
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: NO]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
    return success;
}

You're need to launch this method on every directory and file you're downloading/creating in Documents or Library directory. 您需要在Documents / Library目录中下载/创建的每个目录和文件上启动此方法。

I don't believe that you have any control over when the sync will occur when using iCloud and core-data. 在使用iCloud和核心数据时,我不相信您可以控制何时发生同步。 With UIDocument and iCloud I have used startDownloadingUbiquitousItemAtURL with varying degrees of success but for core-data I don't think there is any equivalent -- I would love to hear someone tell me otherwise however... 使用UIDocument和iCloud,我使用了startDownloadingUbiquitousItemAtURL并取得了不同程度的成功,但对于核心数据,我认为没有任何等价物 - 我很乐意听到有人告诉我,但是...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM