ios - App crashed with UITabBarController and In App Purchases -


my app used in app purchases , references here.

when loading products server block, @ same time switch other tab inside uitabbarcontroller , app crashed when products loaded

this code

//load products server [[lainapphelper sharedinstance] requestproductswithcompletionhandler:^(bool success, nsarray *products) {     if (success) {         // nothing in here app till crashed     } }]; 

if delete lines can switch between tab. nothing throw app when crashed, enable zombie objects. bad access

there problem in implementation of lainapphelper in tutorial linked: helper treats application non-concurrent.

here going on: shared instance of lainapphelper has sharedinstance, owns _completionhandler (among other things).

the requestproductswithcompletionhandler: method assigns _completionhandler copy of block has been passed in. ok first request, if request "in flight", completion block of other request released arc due reassignment. if tab switch starts concurrent request, initial request come released block, causing undefined behavior, , possibly crash.

to fix problem, need split class in 2 - 1 part holding items common requests (namely, _productidentifiers , _purchasedproductidentifiers) , request-specific ones (_productsrequest , _completionhandler).

the instance of first class (let's call lainapphelper) remains shared; instances of second class (let's call lainapphelperrequest) created per-request inside requestproductswithcompletionhandler: method.

-(id)initwithhelper:(lainapphelper*)helper     andcompletionhandler:(requestproductscompletionhandler)completionhandler {     if (self = [super init]) {         _completionhandler = [completionhandler copy];         _productsrequest = [[skproductsrequest alloc] initwithproductidentifiers:helper.productidentifiers]; // need make productidentifiers property         _productsrequest.delegate = self;         [_productsrequest start];     }     return self; } 

you need create block wraps _completionhandler, too, this:

- (void)requestproductswithcompletionhandler:(requestproductscompletionhandler)completionhandler {     __block lainapphelperrequest *req = [[lainapphelperrequest alloc] initwithhelper:self andcompletionhandler:^(bool success, nsarray *products) {          completionhandler(success, products);          req = nil;     }]; } 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -