【问题标题】:Azure Mobile Services - Duplicate item after synchronizationAzure 移动服务 - 同步后重复项目
【发布时间】:2015-12-10 18:17:18
【问题描述】:

我使用 Azure 移动服务作为 iOS 应用程序的后端。我已将所有内容设置为使用离线同步,即使没有网络连接,我也可以查看、添加或修改数据。将新对象添加到表中时遇到问题。添加在本地运行良好,但是当我同步数据时,它会在本地数据库上创建一个重复的项目,其 objectId 略有不同。创建的项目在服务器端不重复。

这是我的设置方式。顺便说一句,感谢 @TheBasicMind 发布这个模型。

这是他对模型的解释的链接:enter link description here

这是我设置同步上下文和同步表的操作:

    // Initialize the Mobile Service client with your URL and key
    MSClient *client = self.hpc.client;

    NSManagedObjectContext *context = self.hpc.syncContext;
    MSCoreDataStore *store = [[MSCoreDataStore alloc] initWithManagedObjectContext:context];

    client.syncContext = [[MSSyncContext alloc] initWithDelegate:syncDelegate dataSource:store callback:nil];

    // Add a Mobile Service filter to enable the busy indicator
    self.client = [client clientWithFilter:self];

    // Create an MSSyncTable instance to allow us to work with the Athlete table
    self.syncAthleteTable = [self.client syncTableWithName:@"Athlete"];

下面是我暂时添加记录的方法:

NSDictionary *newItem = @{@"firstname": firstname, @"lastname": lastname, @"laterality" : laterality};

[self.athletesService addItem:newItem completion:^{

    NSLog(@"New athlete added");
}];

-(void)addItem:(NSDictionary *)item completion:(CompletionBlock)completion
{
    // Insert the item into the Athlete table
    [self.syncAthleteTable insert:item completion:^(NSDictionary *result, NSError *error)
     {
         [self logErrorIfNotNil:error];

         // Let the caller know that we finished
         dispatch_async(dispatch_get_main_queue(), ^{
             completion();
         });
     }];
}

添加按预期工作,它被添加到 UITableView 中,因为我有一个 NSFetchedResultsController 监听我的主上下文。

这就是问题所在。当我使用此功能与服务器同步数据时:

-(void)syncData:(CompletionBlock)completion
{

    // push all changes in the sync context, then pull new data
    [self.client.syncContext pushWithCompletion:^(NSError *error) {
        [self logErrorIfNotNil:error];
        [self pullData:completion];
    }];
}

-(void)pullData:(CompletionBlock)completion
{
     MSQuery *query = [self.syncAthleteTable query];

    // Pulls data from the remote server into the local table.
    // We're pulling all items and filtering in the view
    // query ID is used for incremental sync
    [self.syncAthleteTable pullWithQuery:query queryId:@"allAthletes" completion:^(NSError *error) {
        [self logErrorIfNotNil:error];
        [self refreshDataOnSuccess:completion];

    }];
 }

- (void) refreshDataOnSuccess:(CompletionBlock)completion
{
     MSQuery *query = [self.syncAthleteTable query];

     [query readWithCompletion:^(MSQueryResult *results, NSError *error) {
         [self logErrorIfNotNil:error];

         NSLog(@"Data that pulled from local store: ");
         for ( NSDictionary *dict in results.items ) {
            NSLog(@"%@ %@", [dict objectForKey:@"firstname"], [dict objectForKey:@"lastname"] );
        }

        // Let the caller know that we finished
        dispatch_async(dispatch_get_main_queue(), ^{
            completion();
        });
    }];
}

在同步之后,NSFetchedResultsChangeInsert 被第二次调用,用于具有稍微不同的 objectID 的相同记录。下面是第一个和第二个 objectID 的示例: tD7ADE77E-0ED0-4055-BAF6-B6CF8A6960AE9
tD7ADE77E-0ED0-4055-BAF6-B6CF8A6960AE11
我被困在这里了。

非常感谢任何帮助。谢谢!

【问题讨论】:

    标签: ios azure azure-mobile-services


    【解决方案1】:

    过去,当我看到这种情况发生时,这是因为客户端发送的“id”字段被服务器逻辑更改或忽略了。

    商店在本地使用该字段在核心数据中查找对象,因此对其进行更改可能会导致客户端 SDK 认为它需要插入新对象而不是更新现有对象。

    确认这一点的一种简单方法是使用数据委托上的 tableOperation:complete: 方法,并比较最初的项目与操作执行返回的项目之间的“id”列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多