NSThread vs. NSOperationQueue vs. ??? on the iPhone

前端 未结 2 2024
一个人的身影
一个人的身影 2020-12-04 11:38

Currently I\'m using NSThread to cache images in another thread.

[NSThread detachNewThreadSelector:@selector(cacheImage:) toTarget:self withObje         


        
2条回答
  •  没有蜡笔的小新
    2020-12-04 11:56

    In general you'll get better mileage with NSOperationQueue.

    Three specific reasons:

    • You may want to initiate caching of many items at once. NSOperationQueue is smart enough to only create about as many threads as there are cores, queuing the remaining operations. With NSThread, creating 100 threads to cache 100 images is probably overkill and somewhat inefficient.
    • You may want to cancel the cacheImage operation. Implementing cancellation is easier with NSOperationQueue; most the work is already done for you.
    • NSOperationQueue is free to switch to a smarter implementation (like Grand Central Dispatch) now or in the future. NSThread is more likely to always be just an operating system thread.

    Bonus:

    • NSOperationQueue has some other nice constructs built-in, such as a sophisticated way of honoring operation priorities and dependencies.

提交回复
热议问题