我希望块外的NSlog可以有价值。 我不知道如何解决它。我希望loadPuppiesFromJSON能够正常工作。 有人可以帮帮我!?THX !!
- (id)init
{
if((self = [super init]))
{
allPuppies=[self loadPuppiesFromJSON];
}
return self;
}
- (NSArray *)loadPuppiesFromJSON
{
PFQuery *query = [PFQuery queryWithClassName:@"Information"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
// Do something with the found objects
for (PFObject *object in objects) {
Fish *fish = [[Fish alloc]init];
fish.price = object[@"price"];
fish.name = object[@"name"];
[object saveInBackground];
self.allFishes = objects;
NSLog(@"%@",allFishes);<-----here have some value
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
NSLog(@"%@",allFishes);<----here doesn't have value
}
答案 0 :(得分:1)
您的NSLog(@"%@",allFishes);
没有值的原因是它在您的查询完成之前被调用。它是Race Condition,在运行时这是程序认为你想要的东西:
由于您还想要返回值,请尝试使用 - 只需从块外部移除NSLost并使用返回值调用第二个函数以继续逻辑:
- (void)loadPuppiesFromJSON
{
PFQuery *query = [PFQuery queryWithClassName:@"Information"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
// Do something with the found objects
for (PFObject *object in objects) {
Fish *fish = [[Fish alloc]init];
fish.price = object[@"price"];
fish.name = object[@"name"];
[object saveInBackground];
}
self.allFishes = objects;
NSLog(@"%@",allFishes);<-----here have some value
[self processPuppiesArray:objects];
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
//No NSLog out here - it won't do anything
}
- (void)processPuppiesArray:(NSArray *)puppiesArray
{
//Continue processing here...
}
答案 1 :(得分:0)
您没有获得任何值的NSLog是因为在完成函数执行后在后台调用了块。这就是为什么在执行阻止之前,它完成了你的函数执行,在此期间allFishes变量为空/零。一旦块执行开始并在此变量获得值之后将值赋给allFishes。 如果你需要在allFishes之后执行一些代码,那么创建一个函数并在blockexecution完成之后调用它,如下所示:
- (NSArray *)loadPuppiesFromJSON
{
PFQuery *query = [PFQuery queryWithClassName:@"Information"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
// Do something with the found objects
for (PFObject *object in objects) {
Fish *fish = [[Fish alloc]init];
fish.price = object[@"price"];
fish.name = object[@"name"];
[object saveInBackground];
self.allFishes = objects;
NSLog(@"%@",allFishes);<-----here have some value
[self dataLoadFinished];
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
NSLog(@"%@",allFishes);<----here doesn't have value
}
-(void) dataLoadFinished {
NSLog(@"%@",self.allFishes);<----here you will have value
}
答案 2 :(得分:0)
您没有检索任何数据的原因是您尝试从函数中检索数组但从不返回任何内容。
当你使用 - (NSArray *)loadPuppiesFromJSON时,你实际上必须返回一个你没有做的数组。
例如:
-(NSArray *)loadPuppiesFromJson {
NSArray *allFishes;
allFishes = @[@"someinfo1",@"someinfo2",@"someinfo3"];
return allFishes; // this is where your getting your error and need for it to return anything
}
我建议将你的代码绑定到像这样的void函数中:
-(void)loadPuppiesFromJSON {
PFQuery *query = [PFQuery queryWithClassName:@"Information"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
self.allFishes = objects;
for (PFObject *object in objects) {
Fish *fish = [[Fish alloc]init];
fish.price = object[@"price"];
fish.name = object[@"name"];
[object saveInBackground];
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
NSLog(@"%@",allFishes);<----here has value now
}
并在viewDidLoad或viewDidAppear或您需要的任何地方调用它:
[self loadPuppiesFromJSON]