post-thumb

How to avoid iOS App exceptions at app lunchtime?

Developing iOS applications usually encounters an unexpected exception when dealing with network connections.

This is due to the fact that the UI interface could not wait until the TCP connection turning to timeout.

Its maximal waiting time is 12s.

If an app starts and has a huge amount of data to process, it may cause an exception at app lunchtime.  

The practical solution is to invoke a background thread to process some time-consuming tasks such as querying, setting up a database, or invoking a TCP connection in unblocking mode.

  • Invoke a background thread to process some important tasks
// Task function
-(void) startSyncFriendList {
    // Invoke background thread
    dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_PRIORITY_DEFAULT);
    dispatch_async(queue, ^{

        // Do some tasks
        [self queryMyFrinedList];

    });

    dispatch_release(queue);
}

  • Using unblocking mode to deal with tcp connection to avoid the lock in UI