• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - Azure 移动服务 - 同步后重复项目

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:16:57 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

这是他对模型的解释的链接: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)addItemNSDictionary *)item completionCompletionBlock)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)syncDataCompletionBlock)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)pullDataCompletionBlock)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) refreshDataOnSuccessCompletionBlock)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
我被困在这里了。

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



Best Answer-推荐答案


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

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

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

关于ios - Azure 移动服务 - 同步后重复项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32570006/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap