UITextView textViewDidChange-从后端获取数据,如UISearch

时间:2014-02-23 09:42:00

标签: ios objective-c objective-c-blocks parse-platform

我调用- (void)textViewDidChange:(UITextView *)textView委托,在其中,代码从我的parse.com后端获取数据。但99/100尝试我得到一个空数组,在极少数情况下它会返回确切的数据,我在tableview中列出它。任何重新定义我的代码的最佳方法?或使用任何其他代表?

  - (void)textViewDidChange:(UITextView *)textView

    {

  [searchAskerArray removeAllObjects];

    [searchResultArray removeAllObjects];
    NSString *inComingText = _txtQuestion.text;
    if(![inComingText isEqualToString:@" "] && inComingText.length>4 && [[inComingText componentsSeparatedByString:@" "] count]>0)
    {
            PFQuery *searchQuery = [PFQuery queryWithClassName:@"Questions"];
            _txtQuestion.text = inComingText;
            [searchQuery includeKey:@"user"];

        [searchQuery whereKey:@"question" containsString:_txtQuestion.text];

    [searchQuery findObjectsInBackgroundWithBlock:^(NSArray *quesArray, NSError *error)
     {

         for (int i = 0; i <quesArray.count; i++)
         {
             NSString *ques  = [quesArray[i] objectForKey:@"question"];
             PFUser *asker = [quesArray[i] objectForKey:@"user"];
             [searchResultArray addObject:ques];
             [searchAskerArray addObject:asker.username];
         }
         if (searchResultArray!=nil && searchAskerArray.count >0)
         {
             finalQuesArray = searchResultArray;
             finalAskerArray = searchAskerArray;
             [_similarTable reloadData];

         }
     }];
}}

当用户快速键入时,此委托会立即触发许多事件。任何建议?

1 个答案:

答案 0 :(得分:0)

当搜索查询在后台运行时,您是否等待了一些时间来完成该块,以便通过放置断点或使用NSLog来查看响应。