Daiwei
Daiwei

Reputation: 43596

Could `dispatch_once` created singleton got released? If so, how to deal with it?

As mentioned in this discussion, dispatch_once is very useful in building singleton. However, could dispatch_once created instance got released and dispatch_once was not able to created that instance because it only execute once?

If so, what is the best practice to deal with it?

Upvotes: 2

Views: 185

Answers (1)

nhgrif
nhgrif

Reputation: 62062

Yes.

First best practice is generally going to be to avoid the singleton in the first place.

You can also use the @synchronized(self) pattern in place of the dispatch_once pattern to guarantee only a single object exists at one time, but that it can be re-instantiated if it is ever deallocated for any reason.

You could also reset your dispatch_once variable in dealloc, I think

Upvotes: 1

Related Questions