如何在执行其他任务之前确保查询已完成(无论是否成功)。
这是我的viewDidLoad ...最后的这行代码
NSLog(@"tweets Array %@",self.tweetsArray);
显示tweetsArray为空
然而,上面的块中的相同代码行显示数组已满。
输出还显示第一个NSLog之前的最后一个NSLog。
我的tableView在模拟器上是空白的,因为它在完成运行查询之前调用了UITableView方法。
对此的任何帮助都会很棒。
先谢谢你......尼克
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.selectString = self.gamesString;
PFQuery *query = [PFQuery queryWithClassName:@"Parse_Storyw"];
[query whereKey:@"searchName" equalTo:self.gamesString];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(@"%@", object.objectId);
PFQuery *query = [PFQuery queryWithClassName:@"Parse_Story"];
[query getObjectInBackgroundWithId:object.objectId block:^(PFObject *content, NSError *error) {
PFFile *file = [content objectForKey:@"levels"];
[file saveInBackground];
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
NSString *string = [[NSString alloc] initWithData: data encoding: NSMacOSRomanStringEncoding];
self.tweetsArray = [[NSArray alloc] initWithArray:[string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];
NSLog(@"tweets Array %@",self.tweetsArray);
}];
}];
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
self.title = self.selectString;
NSLog(@"tweets Array %@",self.tweetsArray);
}
答案 0 :(得分:1)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.selectString = self.gamesString;
PFQuery *query = [PFQuery queryWithClassName:@"Parse_Storyw"];
[query whereKey:@"searchName" equalTo:self.gamesString];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(@"%@", object.objectId);
PFQuery *query = [PFQuery queryWithClassName:@"Parse_Story"];
[query getObjectInBackgroundWithId:object.objectId block:^(PFObject *content, NSError *error) {
PFFile *file = [content objectForKey:@"levels"];
[file saveInBackground];
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
NSString *string = [[NSString alloc] initWithData: data encoding: NSMacOSRomanStringEncoding];
self.tweetsArray = [[NSArray alloc] initWithArray:[string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];
NSLog(@"tweets Array %@",self.tweetsArray);
self.tableView.dataSource = self;
self.tableView.delegate = self;
}];
}];
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
self.title = self.selectString;
NSLog(@"tweets Array %@",self.tweetsArray);
试试这个..
答案 1 :(得分:0)
在你的代码里面forloop你继续提升 self.tweetsArray
self.tweetsArray = [[NSArray alloc] initWithArray:[string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];
每次数组都加载新数据时,在此行中。因此,如果您想要更改tableView内容,请动态使用 [yourTableView reloadData] 。
如果 self.tweetsArray 内容已修改,则必须使用 [self.tableView reloadData] 来影响TableView中的更改。