ios - Never getting out of while loop -


i have method needs wait method finish loading objects datastore. i'm using while loop this:

-(void) findagoodcar {     [self.indicator startanimating];      while(appdelegate.availablecars == nil || [appdelegate.availablecars count] < 1) {         sleep(0.01);     }      // stuff } 

while starts working other process, loading cars, has started.

now, indicator never starts animating. loading of objects never finishes. loop never ends. why?

edit: loadcars method. called appdelegate when app starts, (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions , doing query parse.com's cloud asynchronously:

+(void) loadcars {     pfquery *query = [pfquery querywithclassname:kcarobjectkey];     [query wherekey:kactivekey equalto:[nsnumber numberwithinteger:kactivenumber]];     nssortdescriptor *sortbyprice = [[nssortdescriptor alloc] initwithkey:kpricekey ascending:yes];     [query orderbysortdescriptors:[nsarray arraywithobjects:sortbyprice, nil]];      [query findobjectsinbackgroundwithblock:^(nsarray *objects, nserror *error) {         if(!error) {             appdelegate *appdelegate = (appdelegate*)[[uiapplication sharedapplication] delegate];             appdelegate.availablecars = objects;          } else {             nslog(@"error in loadcars %@", error.localizeddescription);         }     }]; } 

here updated post, because real author's problem not "infinite loop".

working parse

parse provides 2 ways of getting/putting data servers:

  • sync
  • async

sync

drawbacks of sync method obvious - blocks ui thread , user see unresponsive ui.

then why group of methods inroduced?

there cases in app's login when can't continue working without knowing result. in case have use sync methods.

async

async approach doesn't block ui. requires deeper understaning of process.

of course, can use such loops:

while(appdelegate.availablecars == nil || [appdelegate.availablecars count] < 1) {     sleep(0.01); } 

that approach give result, block ui thread. using such approach async operation bad practive. should consider using block handling asynchronyous requests.

app's architecture

here point of view on app's architecture. prefer isolating code, working parse (and other apis) separate classes. make class, , make singleton

parseclient.h file:

@interface parseclient : nsobject // singleton + (instancetype) sharedinstance; @end 

parseclient.m file:

@implementation parseclient + (instancetype) sharedinstance {     static parseclient *sharedinstace = nil;     static dispatch_once_t oncetoken;     dispatch_once(&oncetoken, ^{         sharedinstace = [[parseclient alloc] init];     });      return sharedinstace; } @end 

so have access parse object in whole app. can add required function class. can add cache (for example, store array instance variable) , make own cache policy.

for example, can add following function parseclient:

- (void) loadcarswithcompletionblock:(void (^)(nsarray *objects, nserror *error))completionblock {     pfquery *query = [pfquery querywithclassname:kcarobjectkey];     [query wherekey:kactivekey equalto:[nsnumber numberwithinteger:kactivenumber]];     nssortdescriptor *sortbyprice = [[nssortdescriptor alloc] initwithkey:kpricekey ascending:yes];     [query orderbysortdescriptors:[nsarray arraywithobjects:sortbyprice, nil]];      [query findobjectsinbackgroundwithblock:completionblock]; } 

and call in anywhere in app

- (void) foo {     ...     [[parseclient sharedinstance] loadcarswithcompletionblock:^(nsarray *objects, nserror *error){         // have array of cars here      }];     ... } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -