2015-02-08 58 views
0

我有一個使用解析查詢獲取成員列表的函數。我有一個NSMutableArray類型的屬性,它應該存儲名爲訂戶的列表結果。我有兩個代碼塊。一個塊獲取日期,一旦我有日期,我會調用另一個函數,它進一步調用分析雲方法。下面是代碼目標C中的塊/解析查詢

-(void) fetchMemberList{ 

PFQuery *query=[PFQuery queryWithClassName:@"GroupMembers"]; 
[query fromLocalDatastore]; 
[query orderByAscending:@"updatedAt"]; 
[query whereKey:@"iosUserID" equalTo:[PFUser currentUser].objectId]; 
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
if(error) 
{ 

} 
else{ 

    if(objects.count==0) { 
     _latestTime=[PFUser currentUser].createdAt; 
    } 

    else { 
     PFObject *member=[objects objectAtIndex:0]; 
     _latestTime =member.createdAt; 
    } 

    [Data getMemberList:_latestTime successBlock:^(id object) { 

     NSMutableDictionary *members = (NSMutableDictionary *) object; 

     NSArray *appUser= [members objectForKey:@"app"]; 
     NSArray *phoneUser=[members objectForKey:@"sms"]; 
     if(appUser.count>0){ 
     for(PFObject * appUs in appUser) 
     { 
      appUs[@"iosUserID"]=[PFUser currentUser].objectId; 
      NSString *obj= [appUs objectForKey:@"name"]; 
      NSString *child= [appUs objectForKey:@"childern_names"]; 
      if(child.length>0) 
      { 
       [_subscriber addObject:child]; 
      } 
      else if(obj.length>0) 
      { 
       [_subscriber addObject:obj]; 

      } 


      } 
      [PFObject pinAllInBackground:appUser]; 

     } 

     if(phoneUser.count>0){ 
     for(PFObject * phoneUs in phoneUser) 
     { 
      phoneUs[@"iosUserID"]=[PFUser currentUser].objectId; 
      NSString *obj= [phoneUs objectForKey:@"subscriber"]; 
      NSString *child= [phoneUs objectForKey:@"number"]; 
      if(child.length>0) 
      { 
       [_subscriber addObject:child]; 
      } 
      else if(obj.length>0) 
      { 
       [_subscriber addObject:obj]; 

      } 

     } 
      [PFObject pinAllInBackground:phoneUser]; 

     } 


     NSLog(@"%@ subscriber in fetch method",_subscriber); 
    } errorBlock:^(NSError *error) { 

    }]; 


} 


}]; 


} 

在我viewDidLoad中功能: -

- (void)viewDidLoad { 
[super viewDidLoad]; 
_subscriber=[[NSMutableArray alloc]init]; 


[self fetchMemberList]; 
NSLog(@"%@ in viewdidLoad",_subscriber); 

}

所以在日誌中viewDidLoad中不打印任何東西,但一個在方法給出名稱列表。我想存儲列表中的數組,並將其放入tableview中。我在這裏錯過了什麼?

+0

這是異步過程的本質。如果你被搞砸了,那就儘量避免塊。你的代碼可能會變長,但最終會更好理解,特別是當你的塊變得如此龐大時。 – 2015-02-08 10:01:14

回答

0

findObjectsInBackgroundWithBlock:塊是異步的,所以_subscriber isn't immediately available to print after you call your fetchMemberList方法。這就是爲什麼塊中的那個實際上給了你名字的列表 - 因爲那時回調完成了。