【问题标题】:How to call same service multiple time and store data in ios如何多次调用同一个服务并将数据存储在ios中
【发布时间】:2019-07-16 13:18:02
【问题描述】:

我有一种情况,我将从 Web 服务获取超过 25000 条记录,它使用分页技术发送。 所以问题是我只想存储数据,因此我正在考虑循环运行它,但未来的记录可能会有所不同(即 30000,50000 等)

从后端我得到每页 10000 条记录,但我不知道我已经运行了多少次循环所以我该如何处理这个问题?

-(void)vendorsListCalling:(NSInteger)pageIndex{
    [[ServicesHandler new] callVendorDetailsServiceWithParams:@{@"pageno":@(pageIndex)} CompletionBLock:^(NSDictionary *response, NSError *error) {
        if (error) {
            NSLog(@"error log  %@",error.localizedDescription);

        }else{
            NSDictionary *dict = response[@"params"][@"data"];
            [vendorDictionay addEntriesFromDictionary:dict];
            pageCount++;
            [[NSUserDefaults standardUserDefaults] setObject:vendorDictionay forKey:@"vendorsDict"];

        }
    }];

}

上面是我卡住的地方。

任何建议将不胜感激。

【问题讨论】:

  • 要求后端团队发送任何其他带有数据的值(对象)以识别总记录。对象可以是布尔型或整数。根据该值,您可以停止循环。

标签: ios objective-c web-services afnetworking


【解决方案1】:

您可以将数据存储到 sqlite 数据库中。对于服务的递归调用,您可以修改相同的方法,

-(void)vendorsListCalling:(NSInteger)pageIndex {

    if (!loader) {
        //Write code to Show your loader here
    }

    [[ServicesHandler new] callVendorDetailsServiceWithParams:@{@"pageno":@(pageIndex)} CompletionBLock:^(NSDictionary *response, NSError *error) {
        if (error) {
            NSLog(@"error log  %@",error.localizedDescription);

            //If it fails you need to call the service again with the same Index
            [self vendorsListCalling:pageCount];

        } else {
            if (!response[@"params"][@"data"]) {
                //Stop loader since you didn't received any data
            } else {
                NSDictionary *dict = response[@"params"][@"data"];
                [vendorDictionay addEntriesFromDictionary:dict];
                pageCount++;

                // Store Data in database here //

                //Call service with incremented Index
                [self vendorsListCalling:pageCount];
            }
        }
    }];
}

【讨论】:

  • 数据只是字典,我可以使用 userdefaults 吗?
  • 因为你有大量的数据。如果你能把它存储在 DB 或 CoreData 中会更好。
  • @manissharma93 它正在工作,但需要花费大量时间,并且应用程序在此处冻结。
  • @zack 所以你可以做的是让你的通话异步或显示一些进度 Hud,说更新数据或其他东西
  • 调用很好,但存储在数据库中需要很多时间。
猜你喜欢
  • 2016-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
  • 2014-02-22
  • 1970-01-01
相关资源
最近更新 更多