How to get iCloud to sync with basic data?

I use iCloud with basic data to synchronize the data of my application.

I tried everything and finally works great.

but I have one question.

Is there a way to tell iCloud to sync?

Synchronization at application launch, but at a different time. It seems to sync randomly. I can’t find a way to handle this myself.

any help?

Thank.

+5
source share
3 answers

, iCloud. "" "" Ubiquity. . , syncCloud. "" , "" . , iCloud , iCloud. , , , Xcode , "Trigger iCloud Sync".

@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;
        }
    });
}
+1

NSURL iCloud :

- (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;
}

, / "" "".

0

I do not think that you have control over when synchronization will occur when using iCloud and basic data. With UIDocument and iCloud, I used startDownloadingUbiquitousItemAtURL with varying degrees of success, but for basic data, I don’t think there is any equivalent - I would like to hear someone tell me differently ...

0
source

All Articles