Tôi đã tìm ra cách để buộc một kho dữ liệu cốt lõi đồng bộ hóa với iCloud. Bạn chỉ cần "chạm" bất kỳ tập tin biên nhận nào trong thư mục Dữ liệu trong hệ thống tập tin Ubiquity. Mã dưới đây sẽ làm điều này. Để buộc đồng bộ hóa, hãy gọi phương thức syncCloud. Đối với mỗi "nhận" tập tin nó tìm thấy, nó sẽ cập nhật các tập tin tem thời gian sửa đổi cho thời gian hiện tại, về cơ bản làm một "liên lạc" trên tập tin. Ngay khi điều này xảy ra, iCloud sẽ kiểm tra và đồng bộ hóa bất kỳ tệp dữ liệu cốt lõi nào cần đồng bộ hóa trong iCloud. Gần như tôi có thể nói, đây là cách trình mô phỏng hoặc Xcode thực hiện khi bạn chọn tùy chọn "Kích hoạt 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;
}
});
}
Nguồn
2016-01-29 04:49:00
Câu hỏi tương tự .. Tôi cũng muốn buộc đồng bộ hóa –