
iOS7ベースに、アプリのプログラムをしていたら、
iOS4.3ではよく落ちる。
落ちる原因の1つ、
[NSURLConnection sendAsynchronousRequest:request queue:mainQueue completionHandler:completionBlock]
を解決しました。
まずはメソッドを追加します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | -( void )sendAsynchronousRequest:( NSURLRequest *)request queue :( NSOperationQueue *)queue completionHandler :( void (^)( NSURLResponse *response, NSData *data, NSError *error))handler { __block NSURLResponse *response = nil ; __block NSError *error = nil ; __block NSData *data = nil ; // Wrap up synchronous request within a block operation NSBlockOperation *blockOperation = [ NSBlockOperation blockOperationWithBlock :^{ data = [ NSURLConnection sendSynchronousRequest :request returningResponse :&response error :&error]; }]; // Set completion block // EDIT: Set completion block, perform on main thread for safety blockOperation .completionBlock = ^{ // Perform completion on main queue [[ NSOperationQueue mainQueue ] addOperationWithBlock :^{ handler(response, data, error); }]; }; // (or execute completion block on background thread) // blockOperation.completionBlock = ^{ handler(response, data, error); }; // Execute operation [queue addOperation :blockOperation]; } |
そして、今までの呼び出し元で、
1 2 3 4 5 | if ([ UIDevice currentDevice ] .systemVersion .floatValue < 5 .0 ) { [ self sendAsynchronousRequest :urlRequest queue :[ NSOperationQueue mainQueue ] completionHandler :handler]; } else { [ NSURLConnection sendAsynchronousRequest :urlRequest queue :[ NSOperationQueue mainQueue ] completionHandler :handler]; } |
0 件のコメント:
コメントを投稿