ios - for 循环中的异步任务

标签 ios grand-central-dispatch dispatch-async

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

+(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)method2:(NSString*)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)method3:(NSString*)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)getData:(NSString*)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];

}

最佳答案

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

您怀疑数据库更新可能会默默失败这一事实令人担忧。您是否可能没有检查所有 SQLite 调用的返回码?检查每个 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/

相关文章:

ios - 收到来电的后台通知?

ios - 从 Xcode 和 Application Loader 向 App Store 提交应用程序时出错

ios - 绘图区域填充以防止符号被截断。 (核心剧情)

c - GCD 中的定时锁?

iPhone - GCD 检查队列是否暂停

ios - 如何在后台运行代码?使用 dispatch_async 它是如何工作的?

iphone - 访问其他类中的变量时为空值(组合导航和选项卡 Controller )

ios - 为什么此 UI 更新代码有效?

swift - 如何取消调度队列

ios - swift 按顺序同步多个 web 服务调用