Sunday, 29 September 2013

Data was changed during edit it in UIViewController

Data was changed during edit it in UIViewController

I have a multithreading app and I have a problem, I can open an item for
make changes in a UIViewController but the data in another thread can be
changed already, and when I click submit button - I rewrites the changes
made by another thread. (for example the quantity of the item was changed
- someone bought it while admin was changing the quantity of the items in
the warehouse).
So now I have a UIViewController which can edit the item and make changes
like this:
- (IBAction)submitButton:(id)sender
{
NSDictionary *changedData = [NSDictionary
dictionaryWithObjects:[NSArray arrayWithObjects:nameField.text,
priceField.text, quantityField.text, _itemUUID, nil]
forKeys:[NSArray
arrayWithObjects:
@"Name",
@"Price",
@"Quantity",
@"UUID",
nil]];
[[EADataManager sharedInstance] updateItemWithData:changedData
atUUID:_itemUUID];
[self.navigationController popViewControllerAnimated:YES];
}
and the data update method in my dataManager class:
- (void) updateItemWithData:(NSDictionary *)data atUUID:(NSString*)UUID
{
[self networkActivityIndicatorVisible:YES];
dispatch_barrier_async(_dataManagerQueue, ^{
[NSThread sleepForTimeInterval:5.0];
NSInteger path = [self indexFromObjectUUID:UUID];
if (path != NSNotFound)
{
[_items replaceObjectAtIndex:path withObject:data];
[_dataStorageAdapter saveFileWithData:_items];
} else {
[_items addObject:data];
[_dataStorageAdapter saveFileWithData:_items];
}
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter]
postNotificationName:EADataManagerUpdateViews
object:nil];
});
[self networkActivityIndicatorVisible:NO];
});
}
So the item can be edited from different threads or with other methods,
and I don't know how to make this work well. I believe the delegation is
quite good for this but I can't implement it here, any ideas?

No comments:

Post a Comment