2011-12-31 29 views
26

Tôi đang cố gắng để iCloud hoạt động với ứng dụng của mình, cần di chuyển một cửa hàng địa phương hiện có sang một cửa hàng phổ biến, nếu người dùng yêu cầu.Đồng bộ hóa kho dữ liệu Core hiện tại với iCloud

Sau khi một số định hướng xung quanh diễn đàn Apple dev và ở nơi khác, tôi đã thực hiện phương pháp này, không hoạt động liên tục. Tôi đã thực sự nhìn thấy nó hoạt động, nhưng chỉ sau một vài tai nạn trên thiết bị B (được phổ biến từ iCloud).

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 
    if (persistentStoreCoordinator != nil) 
    return persistentStoreCoordinator; 

    NSURL *legacyStoreUrl = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:[self activeStoreFilenameUpgraded:NO]]]; 
    NSURL *upgradedStoreUrl = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:[self activeStoreFilenameUpgraded:YES]]]; 

persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 

    if ((IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")) && (self.iCloudEnabled)) { 
    NSPersistentStoreCoordinator* psc = persistentStoreCoordinator; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    NSDictionary *cloudOptions = nil; 
    NSDictionary *localOptions = [NSDictionary dictionaryWithObjectsAndKeys: 
            [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
            [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
            nil]; 


    NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:@"<CONTAINER ID>"]; 
    NSString *coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:[NSString stringWithFormat:@"logs%d",[self activeStoreIndex]]]; 
    if ([coreDataCloudContent length] != 0) { 
     // iCloud is available 
     cloudURL = [NSURL fileURLWithPath:coreDataCloudContent]; 

     cloudOptions = [NSDictionary dictionaryWithObjectsAndKeys: 
         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
         @"MyAppStore", NSPersistentStoreUbiquitousContentNameKey, 
         cloudURL, NSPersistentStoreUbiquitousContentURLKey, 
         nil]; 
    } else { 
     // iCloud is not available 
    } 

    NSError *error = nil; 
    [psc lock]; 
    if(migrateStore) { 
     migrateStore = NO; 

     NSPersistentStore *srcPS = [psc addPersistentStoreWithType:NSSQLiteStoreType 
      configuration:nil 
      URL:legacyStoreUrl 
      options:localOptions 
      error:&error]; 
     if (![psc migratePersistentStore:srcPS 
      toURL:upgradedStoreUrl 
      options:cloudOptions 
      withType:NSSQLiteStoreType 
      error:&error]) { 
      NSLog(@"Error migrating data: %@, %@/%@/%@", error, [error userInfo], legacyStoreUrl, upgradedStoreUrl); 
      abort(); 
     } 
    } 
    else { 
     if (![psc addPersistentStoreWithType:NSSQLiteStoreType 
      configuration:nil 
      URL:upgradedStoreUrl 
      options:(cloudOptions ? cloudOptions : localOptions) 
      error:&error]) { 
       NSLog(@"Unresolved iCloud error %@, %@", error, [error userInfo]); 
       abort(); 
     } 
    } 
    [psc unlock]; 

    [[NSNotificationCenter defaultCenter] postNotificationName:@"RefetchAllDatabaseData" object:self userInfo:nil]; 
    } else { 
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
          [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
          nil]; 

    NSError *error = nil; 
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:legacyStoreUrl options:options error:&error]) { 
     // error 
     abort(); 
    }  
    } 

    return persistentStoreCoordinator; 
} 
+0

Chỉ cần lưu ý rằng ứng dụng hầu như luôn có chỉ một cửa hàng; mã để xử lý các trường hợp của 2 cửa hàng (activeStoreIndex, vv) là một trường hợp cạnh. – ed94133

+1

Di chuyển sẽ được định nghĩa ở đâu và được chỉ định? – Jim

Trả lời

6

Đồng bộ hóa dữ liệu lõi iCloud bị hỏng hoàn toàn. Một giải pháp đáng tin cậy của bên thứ ba là TICoreDataSync; this fork có hỗ trợ đồng bộ hóa qua iCloud mà không cần dựa vào triển khai đồng bộ hóa Dữ liệu lõi của iCloud. Nó chỉ đơn giản là sử dụng iCloud để đồng bộ hóa các tập tin và xử lý các đối tượng dữ liệu Core đồng bộ trên riêng của mình.

Tôi đã từ bỏ đồng bộ hóa iCloud Core Data và đã xây dựng ứng dụng của tôi với thư viện này thay thế; cho đến nay nó đã được làm việc tuyệt vời, với không có vấn đề tôi thấy với việc thực hiện của Apple.

+0

TICDS không được phát triển tích cực. Tôi đã phát triển một khuôn khổ mới, [Ensembles] (http://ensembles.io), dựa trên các nguyên tắc tương tự và tính toán các bài học kinh nghiệm với TICDS. –

2

tôi đã lên trên iCloud tích hợp, cho bây giờ ít nhất. Tôi không cảm thấy tôi có thể cung cấp cho người dùng một giải pháp đồng bộ đáng tin cậy thông qua iCloud với hiệu suất Dữ liệu cốt lõi thất thường. Tôi đang hy vọng cho iOS 5.1.

+4

Công ty tôi làm việc cho cũng muốn tích hợp iCloud trong một ứng dụng hiện có với dữ liệu cốt lõi, mặc dù tôi đã nói với họ rằng nó không hoạt động đúng. Nó giống như địa ngục !!! Sau khi phát hành vào tháng 2, chúng tôi nhận được rất nhiều phản hồi tiêu cực liên quan đến sự cố và dữ liệu bị mất. Công ty đã mất khả năng thanh toán vào tháng 8: D Chỉ muốn kể câu chuyện ... –

Các vấn đề liên quan