助けてくれてありがとう。ユーザーが質問に答えて、ユーザーのグループに独自の質問を追加できるアプリを作成しようとしています。パースから株式の質問をダウンロードしていますが、さまざまな理由から、これらの質問を特定の順序でメイン配列にロードする必要があります。シリアル 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];
}
}];
}
繰り返しますが、どんな助けも大歓迎です!