Ios 使用解析时,GCD串行队列似乎不会串行执行
提前感谢您的帮助。我正在尝试创建一个应用程序,允许用户回答问题,并为一组人添加自己的问题。我正在从parse下载股票问题,出于各种原因,我需要按照特定顺序将这些问题加载到主数组中。我试图通过串行GCD队列来实现这一点,但这始终是在预期结果和相反结果之间切换。这对我来说毫无意义,但我无法摆脱这样的感觉:它可能与解析查询中使用的异步下载请求有关,而这里的其他答案都没有地址 这是我的密码:Ios 使用解析时,GCD串行队列似乎不会串行执行,ios,objective-c,parse-platform,grand-central-dispatch,serial-processing,Ios,Objective C,Parse Platform,Grand Central Dispatch,Serial Processing,提前感谢您的帮助。我正在尝试创建一个应用程序,允许用户回答问题,并为一组人添加自己的问题。我正在从parse下载股票问题,出于各种原因,我需要按照特定顺序将这些问题加载到主数组中。我试图通过串行GCD队列来实现这一点,但这始终是在预期结果和相反结果之间切换。这对我来说毫无意义,但我无法摆脱这样的感觉:它可能与解析查询中使用的异步下载请求有关,而这里的其他答案都没有地址 这是我的密码: - (void)viewDidLoad{ [super viewDidLoad]; //Load the q
- (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"];
});
}
这是我写的helper方法:
-(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];
}
}];
}
再次感谢您的帮助 这里要做的是序列化对解析库的异步调用。因此,尽管每个-[PFQuery findobjectsinbackgroundithblock:]都是同步排队的,但这些函数本身是异步的。您可以尝试使用-[PFQuery findObjectsWithError:]方法(一种同步方法),以确保方法调用以正确的顺序返回