uitableview - Why is the UITableViewController numberOfRowsInSections called before the init method? -


my uitableviewcontroller won't load information have downloaded parse. know link working because when ask print out pfquery, results want. however, problem method numberofrowsinsection called before init method. i'm not sure how download data before init method.

here .m file

@implementation picklertable  @synthesize picklersarray;  - (id) init {      // call cuperclass's designated init     self = [super initwithstyle:uitableviewstylegrouped];  if(self){      // insert initial data here     pfquery *picklerquery = [pfquery querywithclassname:@"picklers"];      [picklerquery findobjectsinbackgroundwithblock:^(nsarray *objects, nserror *error) {         if (!error) {             picklersarray = [[nsarray alloc] initwitharray:objects];                         }     }];  }  return self; }   -(id) initwithstyle:(uitableviewstyle)style { return [self init]; }  - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {     return picklersarray.count; }  -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {  uitableviewcell *cell = [[uitableviewcell alloc]     initwithstyle:uitableviewcellstyledefault reuseidentifier:@"uitableviewcell"];  pfobject *picklerobject = [picklersarray objectatindex:indexpath.row];     [[cell textlabel] settext:[picklerobject objectforkey:@"title"]];  return cell; } 

i put breakpoint in numberofrowsinsection method , typed bt console. output:

* thread #1: tid = 0x1c03, 0x00003430 tablecontrollerpractice`-[picklertable tableview:numberofrowsinsection:](self=0x07c7d5a0, _cmd=0x01426163, tableview=0x08398a00, section=0) + 64 @ picklertable.m:50, stop reason = breakpoint 1.1 frame #0: 0x00003430 tablecontrollerpractice`-[picklertable tableview:numberofrowsinsection:](self=0x07c7d5a0, _cmd=0x01426163, tableview=0x08398a00, section=0) + 64 @ picklertable.m:50 frame #1: 0x01074548 uikit`-[uisectionrowdata refreshwithsection:tableview:tableviewrowdata:] + 1822 frame #2: 0x01077224 uikit`-[uitableviewrowdata numberofrows] + 98 frame #3: 0x00f3b952 uikit`-[uitableview notenumberofrowschanged] + 120 frame #4: 0x00f3b2dc uikit`-[uitableview reloaddata] + 769 frame #5: 0x00f3edd6 uikit`-[uitableview _reloaddataifneeded] + 65 frame #6: 0x00f43a7e uikit`-[uitableview layoutsubviews] + 36 frame #7: 0x00ee02dd uikit`-[uiview(calayerdelegate) layoutsublayersoflayer:] + 279 frame #8: 0x01d366b0 libobjc.a.dylib`-[nsobject performselector:withobject:] + 70 frame #9: 0x00424fc0 quartzcore`-[calayer layoutsublayers] + 240 frame #10: 0x0041933c quartzcore`ca::layer::layout_if_needed(ca::transaction*) + 468 frame #11: 0x00424eaf quartzcore`-[calayer layoutifneeded] + 166 frame #12: 0x00f7f2bd uikit`-[uiviewcontroller window:setupwithinterfaceorientation:] + 242 frame #13: 0x00ec7b56 uikit`-[uiwindow _setrotatableclient:toorientation:updatestatusbar:duration:force:isrotating:] + 5346 frame #14: 0x00ec666f uikit`-[uiwindow _setrotatableclient:toorientation:updatestatusbar:duration:force:] + 82 frame #15: 0x00ec6589 uikit`-[uiwindow _setrotatablevieworientation:duration:force:] + 89 frame #16: 0x00ec57e4 uikit`__57-[uiwindow _updatetointerfaceorientation:duration:force:]_block_invoke_0 + 224 frame #17: 0x00ec561e uikit`-[uiwindow _updatetointerfaceorientation:duration:force:] + 209 frame #18: 0x00ec63d9 uikit`-[uiwindow setautorotates:forceupdateinterfaceorientation:] + 853 frame #19: 0x00ec92d2 uikit`-[uiwindow setdelegate:] + 351 frame #20: 0x00f7399c uikit`-[uiviewcontroller _trybecomerootviewcontrollerinwindow:] + 164 frame #21: 0x00ec0574 uikit`-[uiwindow addrootviewcontrollerviewifpossible] + 481 frame #22: 0x00ec076f uikit`-[uiwindow _sethidden:forced:] + 368 frame #23: 0x00ec0905 uikit`-[uiwindow _orderfrontwithoutmakingkey] + 49 frame #24: 0x00ec9917 uikit`-[uiwindow makekeyandvisible] + 65 frame #25: 0x00002da6 tablecontrollerpractice`-[homepwnerappdelegate application:didfinishlaunchingwithoptions:](self=0x07c62cb0, _cmd=0x01426c21, application=0x0b05cb60, launchoptions=0x00000000) + 742 @ homepwnerappdelegate.m:33 frame #26: 0x00e8d157 uikit`-[uiapplication _handledelegatecallbackswithoptions:issuspended:restorestate:] + 266 frame #27: 0x00e8d747 uikit`-[uiapplication _callinitializationdelegatesforurl:payload:suspended:] + 1248 frame #28: 0x00e8e94b uikit`-[uiapplication _runwithurl:payload:launchorientation:statusbarstyle:statusbarhidden:] + 805 frame #29: 0x00e9fcb5 uikit`-[uiapplication handleevent:withnewevent:] + 1022 frame #30: 0x00ea0beb uikit`-[uiapplication sendevent:] + 85 frame #31: 0x00e92698 uikit`_uiapplicationhandleevent + 9874 frame #32: 0x0292bdf9 graphicsservices`_purpleeventcallback + 339 frame #33: 0x0292bad0 graphicsservices`purpleeventcallback + 46 frame #34: 0x01e72bf5 corefoundation`__cfrunloop_is_calling_out_to_a_source1_perform_function__ + 53 frame #35: 0x01e72962 corefoundation`__cfrunloopdosource1 + 146 frame #36: 0x01ea3bb6 corefoundation`__cfrunlooprun + 2118 frame #37: 0x01ea2f44 corefoundation`cfrunlooprunspecific + 276 frame #38: 0x01ea2e1b corefoundation`cfrunloopruninmode + 123 frame #39: 0x00e8e17a uikit`-[uiapplication _run] + 774 frame #40: 0x00e8fffc uikit`uiapplicationmain + 1211 frame #41: 0x00002a8d tablecontrollerpractice`main(argc=1, argv=0xbffff2d4) + 141 @ main.m:16 frame #42: 0x000029b5 tablecontrollerpractice`start + 53 

i see in init method:

[picklerquery findobjectsinbackgroundwithblock:^(nsarray *objects, nserror *error) {     if (!error) {         picklersarray = [[nsarray alloc] initwitharray:objects];                     } }]; 

so it's going kick off query on background thread , continue execution on main thread before receives results of query.

later, when query results arrive, run block (on main thread, presume), @ time block sets picklersarray instance variable.

at point, need tell table view reload data. try this:

[picklerquery findobjectsinbackgroundwithblock:^(nsarray *objects, nserror *error) {     if (!error) {         picklersarray = [[nsarray alloc] initwitharray:objects];         [self.tableview reloaddata];     } }]; 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -