曾遇到的問題:封裝SDK中的某個接口時需要上傳地理位置,而獲取地理位置與網(wǎng)絡(luò)請求是個異步操作,也就是說當(dāng)?shù)乩砦恢媚玫降穆恍r,上傳的地理位置會為空。
解決:在網(wǎng)絡(luò)請求之前用下面方法讓線程等待
-(void)waitObject
{
? ? NSRunLoop*runLoop = [NSRunLoopcurrentRunLoop];
? ? isStart=TRUE;
? ? while(isStart==TRUE){
? ? ? ? @autoreleasepool {
? ? ? ? ? ? [runLooprunMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
? ? ? ? }
? ? }
}
當(dāng)在地理位置回調(diào)后 將isStart變量置為FALSE,退出runloop接著就可以同步執(zhí)行網(wǎng)絡(luò)請求的操作了。