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

ios - for循环中的异步任务

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

我希望它们执行三个方法,如下所示:

+(void)method1{
    // Some code
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for (int i=0; i<count; i++) {
            //getting object(x) from json
            [self method2:x];//trigger method2
        }
    });
}

+(void)method2NSString*)x{
// Some code
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for (int i=0; i<count; i++) {
            //getting objects(y,z) from json
            //SAVE that object in SQLite database
            [self method3:y:z];//trigger method3
        }
    });
}

+(void)method3NSString*)y NSString*)z{
// Some code
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for (int i=0; i<count; i++) {
            //getting object from json
            //SAVE that object in SQLite database
        }
    });
}

我所拥有的始终是数据库中的随机数据,此外,并非所有数据都被存储。 我的问题是如何组织这些异步任务以在数据库中获取正确的数据。 非常感谢您的帮助。

编辑:

+(void)getDataNSString*)artist{

    LKDBHelper* globalHelper = [LKDBHelper getUsingLKDBHelper];

    NSMutableArray *arrayOfSongs=[[NSMutableArray alloc]init];
    artist = [artist stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    //DLog(@"artisttt %@",artist);

    NSString *url=[NSString stringWithFormat"%@?artist=%@", @"http://localhost/kalimat/get_kalimat.php",artist];

    url = [url stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];

    //NSLog(@"url %@",url);

    NSURL *urlChannels= [ NSURL URLWithString:url];


    NSURLRequest *request = [NSURLRequest requestWithURL:urlChannels];

    [LKDBHelper clearTableData:[Song class]];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSMutableArray *arrayOfJson=JSON;


            for (int i=0; i<[arrayOfJson count]; i++) {
                //DLog(@"artist songs loop");

                NSMutableDictionary *songDico=[arrayOfJson objectAtIndex:i];

                DCKeyValueObjectMapping *parser = [DCKeyValueObjectMapping mapperForClass: [Song class]];

                Song *song = [parser parseDictionary:songDico];
                song.artist=artist;




                [globalHelper insertToDB:song];

                [self getLyricsWhereArtist:artist andSong:song.song];
            }



        });

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response,
                NSError *error, id JSON) {
        DLog(@"Request Failure Because %@",[error userInfo]);
    }];


    [operation start];

}



Best Answer-推荐答案


如果您有多个线程更新数据库,您可以为您的 SQLite 交互创建一个专用的串行队列,并将所有数据库交互分派(dispatch)到该单个队列。这样,即使您有多个线程执行网络操作,您也可以将所有数据库更新分派(dispatch)到这个新的专用队列,这将完全消除所有数据库争用问题。您可能会遇到数据库更新失败,因为您有多个线程试图更新同一个数据库,其中一些可能会失败。

您怀疑数据库更新可能会静默失败的事实令人担忧。您可能没有检查您的 SQLite 调用的 all 的返回码吗?检查返回码每个 SQLite 调用真的很重要,如果失败,查看sqlite3_errmsg(或者如果使用FMDB,lastErrorMessage) .如果你不这样做,你只是在盲目飞行。幸运的是,此时问题很明显,但下一次问题可能会更微妙,你会费尽心思追查问题。

最后,既然您已经在使用 AFNetworking,我还建议您考虑使用 AFHTTPRequestManager。具体来说,与其自己构建 URL,不如使用 GET 方法,传递 params 字典。您的 stringByAddingPercentEncodingWithAllowedCharacters 代码通常可以工作,但在某些情况下可能会失败(尤其是您的参数值包含 +& 的不太可能的情况它)。此外,如果您使用 GET 并将请求管理器的 operationQueue.maxConcurrentOperationCount 设置为合理的值,例如4,您还将消除在慢速连接上请求不必要地超时的可能性。归根结底,AFHTTPRequestManager 会处理一些微妙的网络问题,但您当前的实现不会。

关于ios - for循环中的异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22748677/

回复

使用道具 举报

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

本版积分规则

关注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