1

I'm trying to compare an NSString to an NSArray from parse with an if statement. Here's what I tried:

-(void)queryParseMethod {
    //PFQuery *query = [PFQuery queryWithClassName:classNameString];
    PFQuery *query = [PFQuery queryWithClassName:@"SaveClass2"];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            _barcodeArray = [[NSArray alloc]initWithArray:objects];
            //[_tableView reloadData];
        }else{
            NSLOG(@"ERROR");
        }
    }];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self queryParseMethod];

    NSIndexPath *indexPath;
    int barcodInt = indexPath.row %1000000;
    PFObject *barcodeObject = [_barcodeArray objectAtIndex:barcodInt];
    NSArray *secondBarcodeArray;
    secondBarcodeArray = [barcodeObject objectForKey:@"RNG1"];

    if ([[secondBarcodeArray objectAtIndex:barcodInt] isEqualToString:_label.text]){
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"FOUND" message:@"THE BARCODE IS CORRECT!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertView show];
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"NOT FOUND" message:@"THE BARCODE IS INCORRECT!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertView show];
    };
}

But it always displays the "NOT FOUND" alert View and I'm sure that the classes are right and the columns are also right but it wont work. Any help will be appreciated.

1
  • have check _barcodeArray.count? Commented Jun 18, 2015 at 10:46

2 Answers 2

1

As long as your _barcodeArray is assigned in background thread (where, I believe runs findObjectsInBackgroundWithBlock:, at point where You're trying to get object barcodeObject from _barcodeArray latter is empty (it can be or can be not). You have classic race condition. If you really need to run this find objects block in background, you should consider using some sync mechanism - like dispatch_semaphore. Or, as I presume, you don't need to call findObjectsInBackgroundWithBlock: and use its synchronous counterpart instead, if present.

Sign up to request clarification or add additional context in comments.

1 Comment

I presume, findObjects: would suffice
1

The blocks are asynchronous so you can't know when block will run on main thread.

I read your code and I found that you are fetching object asynchronously. It means execution wont wait for that method findObjectsInBackgroundWithBlock: to complete.

So the solution is use the synchronous versions of this methods like...

- (NSArray *)findObjects;
- (NSArray *)findObjects:(NSError **)error;

the above method stops the execution further util the findObject: method not get executed.

Use this

-(void)queryParseMethod {
    PFQuery *query = [PFQuery queryWithClassName:@"SaveClass2"];
    NSError *error = nil;
    [query findObjects:&error] {
    if (!error) {
        _barcodeArray = [[NSArray alloc]initWithArray:objects];
    } else {
        //error
    }
}

I hope this will work for you...good luck...!! :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.