Common multithreading mistakes beginners make on iPhone

后端 未结 2 818
栀梦
栀梦 2020-12-13 01:10

I just introduced multithreading into my app JUST to get a silly UIActivityIndicatorView to work. Well, the activity indicator works, alright -- but now my app crashes somet

2条回答
  •  时光说笑
    2020-12-13 01:48

    Probably the most common mistake beginners make (in any language) when working with threads is allowing access to mutable shared resources without guards/mutexes. You guard resources like:

    @synchronized(sharedData)
    {
       // modify sharedData safely
    }
    

    You'll want to limit the amount of data shared between threads and if it must be shared, prefer immutable objects in order to reduce contention caused by synchronization.

    Managing threads is another place where trouble can arise. Here is a document reference specific to the use of threads in the iPhone.

    http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html.

    Without providing code, it's anyone's guess as to what is wrong with your app, but I would start with making sure you are correctly managing the creation and termination of the thread as well as putting particular attention to any shared resources that thread attempts to access.

提交回复
热议问题