gpt4 book ai didi

ios - 使用Parse时,似乎未按顺序执行GCD串行队列

转载 作者:行者123 更新时间:2023-12-01 19:01:31 24 4
gpt4 key购买 nike

在此先感谢您的帮助。我正在尝试创建一个允许用户回答问题并为一群人添加自己的应用的应用。我正在从解析中下载股票问题,出于各种原因,我需要将这些问题以特定顺序加载到主数组中。我正在尝试通过串行GCD队列来完成此操作,但这始终在预期结果和相反结果之间进行切换。这对我来说没有任何意义,但是我无法撼动它可能与解析查询一起使用的异步下载请求有关的感觉,这里没有其他答案。

这是我的代码:

- (void)viewDidLoad{
[super viewDidLoad];


//Load the question with Central Dispatch to ensure load order
dispatch_queue_t my_queue = dispatch_queue_create("com.suresh.methodsqueue", NULL);
dispatch_async(my_queue, ^{
//Load the inital questions
if (self.objectQuestions == nil) {
[self loadQuestionsWithClassName:@"FirstHundred"];
}
});
dispatch_async(my_queue, ^{
//Load the paid questions
[self loadQuestionsWithClassName:@"PaidQuestions"];
});
}

这是我写的辅助方法:
-(void)loadQuestionsWithClassName:(NSString *)tempString {
//Query from Parse
PFQuery *query = [PFQuery queryWithClassName:tempString];
[query orderByAscending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *firstobjects, NSError *error) {
if (!error) {
// The find succeeded. Relay that information.
NSLog(@"Successfully retrieved %lu items from user %@", (unsigned long)firstobjects.count, [[PFUser currentUser] objectId]);

//Clear the objectQuestions temporary array and load the new Questions with the QuestionAndAnswers class
self.objectQuestions = nil;
self.objectQuestions = (NSMutableArray *)firstobjects;
int n;
int x = 0;
int z = (int)[[MainQuestionList sharedInstance].mainQuestionList count];
for (n=(int)[[MainQuestionList sharedInstance].mainQuestionList count]; n<[self.objectQuestions count]+z; ++n)
{
QuestionsAndAnswers *startQuestion = [[QuestionsAndAnswers alloc] init];
startQuestion.questionNumber = &(n)+1;
PFObject *object = [self.objectQuestions objectAtIndex:x];
startQuestion.question = [object objectForKey:@"question"];
startQuestion.answer = 0;
startQuestion.starred = NO;
startQuestion.answered = NO;
startQuestion.category = [object objectForKey:@"Category"];
++x;


[[MainQuestionList sharedInstance].mainQuestionList addObject:startQuestion];
}

//Find the first unanswered question
while ([[MainQuestionList sharedInstance].mainQuestionList[self.mainQuestionNumber] answered] == YES) {
++self.mainQuestionNumber;
};
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:@"We are sorry. Something seems to have gone wrong loading the app. Please check your internet connection and restart the app!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}];

}

再次感谢您的帮助!

最佳答案

您在这里所做的是要序列化对Parse库的异步调用。因此,尽管每个-[PFQuery findObjectsInBackgroundWithBlock:]都被同步排队,但这些函数本身是异步的。您可以尝试使用-[PFQuery findObjectsWithError:]方法(一种同步方法)来确保您的方法调用以正确的顺序返回。

关于ios - 使用Parse时,似乎未按顺序执行GCD串行队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22549027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com