我希望它们执行三个方法,如下所示:
+(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-推荐答案 strong>
如果您有多个线程更新数据库,您可以为您的 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/
|