objective-cmultithreadingnsnotificationcenternsoperationqueuensinvocationoperation

Access to NSUInteger property


I've some problem with accessing NSUInteger property in my code, which look like that:

MyController.h

@interface MyController : UIViewController
  @property (retain, nonatomic) NSArray *updatesArray;

  @property (nonatomic) NSUInteger madeUpdatesCounter;
  @property (nonatomic) NSUInteger allUpdatesCounter;
@end

MyController.m

@implementation MyController

  @synthesize updatesArray;

  @synthesize madeUpdatesCounter;
  @synthesize allUpdatesCounter;

- (void)viewDidLoad
{
    ....
    madeUpdatesCounter = 0;
    allUpdatesCounter = [updatesArray count];

    ....

    // Register for progress notifications    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(makeProgressChange)
                                                 name:@"MadeOneUpdateNotification"
                                               object:nil];
}

- (void)makeProgressChange
{
     madeUpdatesCounter++;
     NSLog(@"Update progress: %d/%d", madeUpdatesCounter, allUpdatesCounter);
}
@end

I'm prosessing my updates as NSInvocationOperation by adding to NSOperationQueue. In the end of the one update action I'm sending notification:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MadeOneUpdateNotification" object:nil];

After executing above code, receiving notifocation is execute only once and in logs I see someting like this:

Update progress: 1/3

When I change line:

allUpdatesCounter = [updatesArray count];

to

allUpdatesCounter = 3;

then everything works ok, and I see in logs: Update progress: 1/3 Update progress: 2/3 Update progress: 3/3

Variable updatedArray is initialized before view is loaded. Is done in this way:

MyController *doUpdatesVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MyController"];
doUpdatesVC.updatesArray = updatesArray;
[self presentViewController:doUpdatesVC animated:YES completion:nil];

Do you have any advices or hints what I'm doing wrong in my code?


Solution

  • OK, I've found reason of my problems. The applications locked by accessing to the same variable by operations launched from queue. When I changed logic of my code, then everything start working correctly.