This is related to a recent thread Update progress with MRProgress. I converted my cloudkit queries from the convenience API to CKOperations as a result of previous thread (Thanks Edwin!). So while using CKModifyRecordsOperation to save a record, I can see the record's progress via logging in the perRecordProgressBlock, which is great. However, I'm trying to send this progress back to the viewcontroller and I cannot figure out how to do that. I have created a class for all of my CloudKit methods - CKManager. The other problem I'm having is that I'm unsure when to update the progress indicator (using MRProgress framework) in the VC. Do I call it before, during or after the save operations call in the CKManager? Should it be called recursively until the progress == 1.0? Here is the code I have so far...every works fine except for updating/animating the progress indicator (it appears and shows 0% and then disappears when the save operation is completed). Also, I'm using a property (double progress) in my CKManager class and I know that is incorrect, but I wasn't sure how else to do it. And I do not feel that the callback method I've declared/defined in my CKManager class below for this is correct either. Any guidance is appreciated!
CKManager.h
#property (nonatomic, readonly) double progress;
- (void)recordProgressWithCompletionHandler:(void (^)(double progress))completionHandler;
CKManager.m
#property (nonatomic, readwrite) double progress;
- (void)recordProgressWithCompletionHandler:(void (^)(double))completionHandler {
completionHandler(self.progress);
}
- (void)saveRecord:(NSArray *)records withCompletionHandler:(void (^)(NSArray *, NSError *))completionHandler {
NSLog(#"INFO: Entered saveRecord...");
CKModifyRecordsOperation *saveOperation = [[CKModifyRecordsOperation alloc] initWithRecordsToSave:records recordIDsToDelete:nil];
saveOperation.perRecordProgressBlock = ^(CKRecord *record, double progress) {
if (progress <= 1) {
NSLog(#"Save progress is: %f", progress);
self.progress = progress;
}
};
saveOperation.perRecordCompletionBlock = ^(CKRecord *record, NSError *error) {
NSLog(#"Save operation completed!");
completionHandler(#[record], error);
};
[self.publicDatabase addOperation:saveOperation];
}
Viewcontroller.m - this is from the method that takes the photo from the camera and calls the CKManager class to prepare the record and save it to CK as well as display the MRProgress indicator...
if (self.imageDataAddedFromCamera) {
self.hud = [MRProgressOverlayView showOverlayAddedTo:self.myCollectionView animated:YES];
self.hud.mode = MRProgressOverlayViewModeDeterminateCircular;
self.hud.titleLabelText = UPLOADING_MSG;
// prepare the CKRecord and save it
[self.ckManager saveRecord:#[[self.ckManager createCKRecordForImage:self.imageDataAddedFromCamera]] withCompletionHandler:^(NSArray *records, NSError *error) {
if (!error && records) {
NSLog(#"INFO: Size of records array returned: %lu", (unsigned long)[records count]);
CKRecord *record = [records lastObject];
self.imageDataAddedFromCamera.recordID = record.recordID.recordName;
NSLog(#"INFO: Record saved successfully for recordID: %#", self.imageDataAddedFromCamera.recordID);
[self.hud dismiss:YES];
[self.hud removeFromSuperview];
[self.imageLoadManager addCIDForNewUserImage:self.imageDataAddedFromCamera]; // update the model with the new image
// update number of items since array set has increased from new photo taken
self.numberOfItemsInSection = [self.imageLoadManager.imageDataArray count];
[self updateUI];
} else {
NSLog(#"Error trying to save the record!");
NSLog(#"ERROR: Error saving record to cloud...%#", error.localizedDescription);
[self.hud dismiss:YES];
[self.hud removeFromSuperview];
[self alertWithTitle:YIKES_TITLE andMessage:ERROR_SAVING_PHOTO_MSG];
}
}];
// where does this call belong?
[self.ckManager recordProgressWithCompletionHandler:^(double progress) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"Updating hud display...");
[self.hud setProgress:progress animated:YES];
});
}];
You should include the progress handler in your saveRecord call like this:
- (void)saveRecord:(NSArray *)records withCompletionHandler:(void (^)(NSArray *, NSError *))completionHandler recordProgressHandler:(void (^)(double))progressHandler {
NSLog(#"INFO: Entered saveRecord...");
CKModifyRecordsOperation *saveOperation = [[CKModifyRecordsOperation alloc] initWithRecordsToSave:records recordIDsToDelete:nil];
saveOperation.perRecordProgressBlock = ^(CKRecord *record, double progress) {
if (progress <= 1) {
NSLog(#"Save progress is: %f", progress);
progressHandler(progress)
}
};
saveOperation.perRecordCompletionBlock = ^(CKRecord *record, NSError *error) {
NSLog(#"Save operation completed!");
completionHandler(#[record], error);
};
[self.publicDatabase addOperation:saveOperation];
}
Then you can call that save record like this:
[self.ckManager saveRecord:#[[self.ckManager createCKRecordForImage:self.imageDataAddedFromCamera]] withCompletionHandler:^(NSArray *records, NSError *error) {
if (!error && records) {
NSLog(#"INFO: Size of records array returned: %lu", (unsigned long)[records count]);
CKRecord *record = [records lastObject];
self.imageDataAddedFromCamera.recordID = record.recordID.recordName;
NSLog(#"INFO: Record saved successfully for recordID: %#", self.imageDataAddedFromCamera.recordID);
[self.hud dismiss:YES];
[self.hud removeFromSuperview];
[self.imageLoadManager addCIDForNewUserImage:self.imageDataAddedFromCamera]; // update the model with the new image
// update number of items since array set has increased from new photo taken
self.numberOfItemsInSection = [self.imageLoadManager.imageDataArray count];
[self updateUI];
} else {
NSLog(#"Error trying to save the record!");
NSLog(#"ERROR: Error saving record to cloud...%#", error.localizedDescription);
[self.hud dismiss:YES];
[self.hud removeFromSuperview];
[self alertWithTitle:YIKES_TITLE andMessage:ERROR_SAVING_PHOTO_MSG];
}
}, recordProgressHandler:^(double progress) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"Updating hud display...");
[self.hud setProgress:progress animated:YES];
});
}];
So that the code for updating the progress is part of your saveRecord call.
The code above is not tested by me. So I hope I made no typo's
Related
I'm trying to persist new entities:
- (void)updateStorageWithQuizzess:(NSArray *)quizzess completion:(void(^)(NSArray *quizzess, BOOL succes, NSError *error))completion {
NSMutableArray *mutableArray = [NSMutableArray array];
[Quiz MR_truncateAll];
[[NSManagedObjectContext MR_context] MR_saveWithBlock:^(NSManagedObjectContext *localContext) {
for (NSDictionary *dictionary in quizzess) {
Quiz *quiz = [Quiz MR_createEntity];
[quiz fromDictionary:dictionary];
[mutableArray addObject:quiz];
}
} completion:^(BOOL contextDidSave, NSError *error) {
BlockSafeRun(completion, mutableArray, contextDidSave, error);
}];
}
Or like this:
- (void)updateStorageWithQuizzess:(NSArray *)quizzess completion:(void(^)(NSArray *quizzess, BOOL succes, NSError *error))completion {
NSMutableArray *mutableArray = [NSMutableArray array];
[Quiz MR_truncateAll];
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
for (NSDictionary *dictionary in quizzess) {
Quiz *quiz = [Quiz MR_createEntity];
[quiz fromDictionary:dictionary];
[mutableArray addObject:quiz];
}
} completion:^(BOOL contextDidSave, NSError *error) {
BlockSafeRun(completion, mutableArray, contextDidSave, error);
}];
}
But in completion block I receive contextDidSave == NO, error == nil.
So I cannot figure out what went wrong.
Are there some obvious mistakes I made? How can I debug that issue?
//////
2015-06-17 20:39:27.061 HITO[6733:618953] Set root saving context: <NSManagedObjectContext: 0x16dbe070>
2015-06-17 20:39:27.062 HITO[6733:618953] Created new main queue context: <NSManagedObjectContext: 0x16e855b0>
2015-06-17 20:39:27.063 HITO[6733:618953] Set default context: <NSManagedObjectContext: 0x16e855b0>
2015-06-17 20:39:27.316 HITO[6733:618953] [HockeySDK] WARNING: Detecting crashes is NOT enabled due to running the app with a debugger attached.
2015-06-17 20:39:28.829 HITO[6733:618953] Created new private queue context: <NSManagedObjectContext: 0x16d57870>
2015-06-17 20:39:28.831 HITO[6733:619027] Created new private queue context: <NSManagedObjectContext: 0x16ea4ec0>
2015-06-17 20:39:28.841 HITO[6733:619027] NO CHANGES IN ** saveWithBlock:completion: ** CONTEXT - NOT SAVING
update
Following code from MR:
- (void) MR_saveWithOptions:(MRSaveOptions)saveOptions completion:(MRSaveCompletionHandler)completion;
{
__block BOOL hasChanges = NO;
if ([self concurrencyType] == NSConfinementConcurrencyType)
{
hasChanges = [self hasChanges];
}
else
{
[self performBlockAndWait:^{
hasChanges = [self hasChanges];
}];
}
if (!hasChanges)
{
MRLogVerbose(#"NO CHANGES IN ** %# ** CONTEXT - NOT SAVING", [self MR_workingName]);
if (completion)
{
dispatch_async(dispatch_get_main_queue(), ^{
completion(NO, nil);
});
}
return;
}
So, hasChanges returns NO.
Your objects have no changes occurring in the save block. There are two problems I see here.
You are creating your new objects in the MR_defaultContext when you need to be creating them in the localContext that is the saving context for your save block.
Try this:
- (void)updateStorageWithQuizzess:(NSArray *)quizzess completion:(void(^)(NSArray *quizzess, BOOL succes, NSError *error))completion {
NSMutableArray *mutableArray = [NSMutableArray array];
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
for (NSDictionary *dictionary in quizzess) {
Quiz *quiz = [Quiz MR_createInContext:localContext];
[quiz fromDictionary:dictionary];
[mutableArray addObject:quiz];
}
} completion:^(BOOL contextDidSave, NSError *error) {
BlockSafeRun(completion, mutableArray, contextDidSave, error);
}];
}
Your objects are not actually being deleted before you start importing the new objects. This probably isn't causing your context to have no changes but I'll discuss this as well.
[Quiz MR_truncateAll] just sets all of your Quiz objects deletedproperty to true. This means the next time that the context is saved or processed, the changes will be saved.
So, when you create your new saving context that context still has those objects. I'm not sure what your fromDictionary method is doing, but if its relying on an database, then it's not going to have it.
You need to do it like this:
- (void)updateStorageWithQuizzess:(NSArray *)quizzess completion:(void(^)(NSArray *quizzess, BOOL succes, NSError *error))completion {
NSMutableArray *mutableArray = [NSMutableArray array];
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
[Quiz MR_truncateAllInContext:localContext];
[localContext processPendingChanges];
for (NSDictionary *dictionary in quizzess) {
Quiz *quiz = [Quiz MR_createInContext:localContext];
[quiz fromDictionary:dictionary];
[mutableArray addObject:quiz];
}
} completion:^(BOOL contextDidSave, NSError *error) {
BlockSafeRun(completion, mutableArray, contextDidSave, error);
}];
}
This way, you are deleting the objects in the saving context. You have to also remember to call processPendingChanges on the saving context to delete the objects from the context, rather than just marking them to be deleted.
I've been writing a camera app for iOS 8 that uses AVFoundation to set up and handle recording and saving (not ImagePickerController). I'm trying to save use the maxRecordedFileSize attribute of the AVCaptureMovieFileOutput class to allow the user to fill up all available space on the phone (minus a 250MB buffer left for apple stuff).
- (unsigned long long) availableFreespaceInMb {
unsigned long long freeSpace;
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
if (dictionary) {
NSNumber *fileSystemFreeSizeInBytes = [dictionary objectForKey: NSFileSystemFreeSize];
freeSpace = [fileSystemFreeSizeInBytes unsignedLongLongValue];
} else {
NSLog(#"Error getting free space");
//Handle error
}
//convert to MB
freeSpace = (freeSpace/1024ll)/1024ll;
freeSpace -= _recordSpaceBufferInMb; // 250 MB
NSLog(#"Remaining space in MB: %llu", freeSpace);
NSLog(#" Diff Since Last: %llu", (_prevRemSpaceMb - freeSpace));
_prevRemSpaceMb = freeSpace;
return freeSpace;
}
The AVErrorMaximumFileSizeReached is thrown when available space (minus buffer) is reduced to zero, and no save error is thrown, but the video does not appear in the camera roll and is not saved. When I set the maxRecordedDuration field the AVErrorMaximumDurationReached is thrown and the video DOES save. I calculate max time from max size, but I always have plenty of space left due to frame compression.
- (void) toggleMovieRecording
{
double factor = 1.0;
if (_currentFramerate == _slowFPS) {
factor = _slowMotionFactor;
}
double availableRecordTimeInSeconds = [self remainingRecordTimeInSeconds] / factor;
unsigned long long remainingSpace = [self availableFreespaceInMb] * 1024 * 1024;
if (![[self movieFileOutput] isRecording]) {
if (availableSpaceInMb < 50) {
NSLog(#"TMR:Not enough space, can't record");
[AVViewController currentVideoOrientation];
[_previewView memoryAlert];
return;
}
}
if (![self enableRecording]) {
return;
}
[[self recordButton] setEnabled:NO];
dispatch_async([self sessionQueue], ^{
if (![[self movieFileOutput] isRecording])
{
if ([[UIDevice currentDevice] isMultitaskingSupported])
{
[self setBackgroundRecordingID:[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]];
}
// Update the orientation on the movie file output video connection before starting recording.
[[[self movieFileOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation: [AVViewController currentVideoOrientation]];//[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] videoOrientation]];
// Start recording to a temporary file.
NSString *outputFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[#"movie" stringByAppendingPathExtension:#"mov"]];
// Is there already a file like this?
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:outputFilePath]) {
NSLog(#"filexists");
NSError *err;
if ([fileManager removeItemAtPath:outputFilePath error:&err] == NO) {
NSLog(#"Error, file exists at path");
}
}
[_previewView startRecording];
// Set the movie file output to stop recording a bit before the phone is full
[_movieFileOutput setMaxRecordedFileSize:remainingSpace]; // Less than the total remaining space
// [_movieFileOutput setMaxRecordedDuration:CMTimeMake(availableRecordTimeInSeconds, 1.0)];
[_movieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:outputFilePath] recordingDelegate:self];
}
else
{
[_previewView stopRecording];
[[self movieFileOutput] stopRecording];
}
});
}
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
NSLog(#"AVViewController: didFinishRecordingToOutputFile");
if (error) {
NSLog(#"%#", error);
NSLog(#"Caught Error");
if ([error code] == AVErrorDiskFull) {
NSLog(#"Caught disk full error");
} else if ([error code] == AVErrorMaximumFileSizeReached) {
NSLog(#"Caught max file size error");
} else if ([error code] == AVErrorMaximumDurationReached) {
NSLog(#"Caught max duration error");
} else {
NSLog(#"Caught other error");
}
[self remainingRecordTimeInSeconds];
dispatch_async(dispatch_get_main_queue(), ^{
[_previewView stopRecording];
[_previewView memoryAlert];
});
}
// Note the backgroundRecordingID for use in the ALAssetsLibrary completion handler to end the background task associated with this recording. This allows a new recording to be started, associated with a new UIBackgroundTaskIdentifier, once the movie file output's -isRecording is back to NO — which happens sometime after this method returns.
UIBackgroundTaskIdentifier backgroundRecordingID = [self backgroundRecordingID];
[self setBackgroundRecordingID:UIBackgroundTaskInvalid];
[[[ALAssetsLibrary alloc] init] writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(#"%#", error);
NSLog(#"Error during write");
} else {
NSLog(#"Writing to photos album");
}
[[NSFileManager defaultManager] removeItemAtURL:outputFileURL error:nil];
if (backgroundRecordingID != UIBackgroundTaskInvalid)
[[UIApplication sharedApplication] endBackgroundTask:backgroundRecordingID];
}];
if (error) {
[_session stopRunning];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), _sessionQueue, ^{
[_session startRunning];
});
}
"Writing to photos album" appears when both errors are thrown. I'm completely stumped by this. Any iOS insights?
The code sample you provided is hard to test since there are a properties and methods missing. Although I am not able to compile your code, there are definitely some red flags that could be causing the issue. The issues below were found inside of:
captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:
Issue 1: the method is handling the passed in error, but then continues executing the method. instead it should be:
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error {
if (error) {
// handle error then bail
return;
}
// continue on
}
Issue 2: The ALAssetsLibrary object that you are instantiating isn't stored to a property, so once captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error: finishes the object will be released (potentially never firing your completion block). Instead it should be:
// hold onto the assets library beyond this scope
self.assetsLibrary = [[ALAssetsLibrary alloc] init];
// get weak reference to self for later removal of the assets library
__weak typeof(self) weakSelf = self;
[self.assetsLibrary writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL, NSError *error) {
// handle error
// handle cleanup
// cleanup new property
weakSelf.assetsLibrary = nil;
}];
If fixing these issues doesn't fix the problem, please provide the missing code to make your sample compile.
I have already configured all Game Center functions and below code i am using to unlock achievement, which is perfectly working fine.
- (void) unlockAchievementThis:(NSString*)achievementID {
GKAchievement *achievement = [[GKAchievement alloc] initWithIdentifier:
achievementID];
if (achievement){
achievement.percentComplete = 100;
achievement.showsCompletionBanner = true;
[GKAchievement reportAchievements:#[achievement] withCompletionHandler:^(NSError *error) {
if (error != nil) {
NSLog(#"Error at unlockAchievementThis()");
}
}];
}
}
Now My problem is with incremental achievements. I have another method for few achievements and I want the previous achievement percentage to increase it with a constant.
My game is in cpp and i don't know much ObjC.
I got some code below which i think should help me but i don't know how to use achievementDescriptions to get percentage and add incStep into it and submit it to back game center.
- (void) incrementAchievementThis:(NSString*)achievementID :(NSInteger) incStep
{
NSMutableDictionary *achievementDescriptions = [[NSMutableDictionary alloc] init];
[GKAchievementDescription loadAchievementDescriptionsWithCompletionHandler:^(NSArray *descriptions, NSError *error) {
if (error != nil) {
NSLog(#"Error getting achievement descriptions: %#", error);
}
for (GKAchievementDescription *achievementDescription in descriptions) {
[achievementDescriptions setObject:achievementDescription forKey:achievementDescription.identifier];
}
}];
Percentages are stored in GKAchievement percentComplete, so you need to load (and update and report) GKAchievements instead of GKAchievementDescriptions.
GKAchievmenentDescriptions are configured in iTunes Connect and are "read-only" from the point of view of your app.
Finally i got output by below code...
- (void) incrementAchievementThis:(NSString*)achievementID :(NSInteger) incStep
{
[GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error)
{
if (error == nil) {
for (GKAchievement* achievement in achievements) {
if ([achievementID isEqualToString:achievement.identifier]) {
achievement.percentComplete += incStep;
[GKAchievement reportAchievements:#[achievement] withCompletionHandler:^(NSError *error) {
if (error != nil) {
NSLog(#"Error at incrementAchievementThis()");
}
}];
}
}
}
else {
NSLog(#"Error in loading achievements: %#", error);
}
}];
}
I've been struggling on this for several days so any would be appreciated. I'm trying to save the players array below and display it in a UITableView. I'd like to save it so I can display the local player's friends. I've tried several different things but something that looks it's working for others is this.
__block NSArray *friends;
- (void) loadPlayerData: (NSArray *) identifiers {
[GKPlayer loadPlayersForIdentifiers:identifiers withCompletionHandler:^(NSArray *players, NSError *error) {
if (error != nil) {
// Handle the error.
}
if (players != nil) {
friends = players;
NSLog(#"Inside: %#", friends); //Properly shows the array
}
}];
NSLog(#"Outside: %#", friends): //Doesn't work, shows nil
}
But friends is still nil/null afterwards. Am I doing something wrong? Is there any way to save players and use it in a UITableView? Thanks.
***EDIT***
So here's the solution I put together.
typedef void(^CallbackBlock)(id object);
+ (void) retrieveFriends: (CallbackBlock)callback {
GKLocalPlayer *lp = [GKLocalPlayer localPlayer];
if (lp.authenticated) {
[lp loadFriendsWithCompletionHandler:^(NSArray *friends, NSError *error) {
if (friends != nil) {
[self loadPlayerDataWithIdentifiers:friends callback:^(NSArray *playersInfo) {
if (callback) callback(playersInfo);
}];
}
}];
}
}
+ (void) loadPlayerDataWithIdentifiers: (NSArray *) identifiers callback:(CallbackBlock)callback {
[GKPlayer loadPlayersForIdentifiers:identifiers withCompletionHandler:^(NSArray *players, NSError *error) {
if (error != nil) {
// Handle the error.
}
if (players != nil) {
if (callback) callback(players);
}
}];
}
The only thing is, my UITableView is in another class so I tried doing this and making the two methods above public. info isn't printing out anything. Any ideas?
[GameCenterHelper retrieveFriends:^(NSArray *info) {
NSLog(#"Friends Info: %#", info);
}];
Use callback blocks.
typedef void(^CallbackBlock)(id object);
- (void)loadPlayerDataWithIdentifiers:(NSArray *)identifiers callback:(CallbackBlock)callback {
[GKPlayer loadPlayersForIdentifiers:identifiers withCompletionHandler:^(NSArray *players, NSError *error) {
if (error != nil) {
// Handle the error.
}
if (callback) callback(players);
}];
}
You imply in your question that this is for a table view. If so, you need to reload your table after the data has been loaded.
[self loadPlayerDataWithIdentifiers:identifiers callback:^(NSArray *players) {
self.players = players;
[self.tableView reloadData];
}];
Crimson Chris is correct.
The other option is use GCD to wait for the response to comeback.
Or change this to synchronous call as you want to get the results immediately.
I am completing some items on a check list. I am counting the completed items using the following method:
- (NSUInteger)completedCount {
return [DIDTask MR_countOfEntitiesWithPredicate:[NSPredicate predicateWithFormat:#"completed == YES && list == %#", self]];
}
The problem I have is when I'm calling the method on a list - list.completedCount - immediately after the data is saved it does not give me the correct count but rather the value - 1. Only after when the app change screen for instance or display a pop-up (as per below), then list.completedCount gives me the correct value. But this is too late for me.
[UIAlertView showAlertViewWithTitle:#"Are you OK?" message:task.name cancelButtonTitle:#"Stop" otherButtonTitles:#[#"Yes", #"No"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
if (buttonIndex > 0) {
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
[[task MR_inContext:localContext] setCompletedValue:buttonIndex == 1];
} completion:^(BOOL success, NSError *error) {
}];
[self continueAutomaticModeWithList:list taskIndex:index + 1];
}
}];
My question is how can I update or refresh the app immediately when the data is saved so that list.completedCount gives me the correct count right away?
It doesn't work, because [self continueAutomaticModeWithList:list taskIndex:index + 1]; is performed before the save completes. You have to move it to the completion block:
[UIAlertView showAlertViewWithTitle:#"Are you OK?" message:task.name cancelButtonTitle:#"Stop" otherButtonTitles:#[#"Yes", #"No"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
if (buttonIndex > 0) {
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
[[task MR_inContext:localContext] setCompletedValue:buttonIndex == 1];
} completion:^(BOOL success, NSError *error) {
[self continueAutomaticModeWithList:list taskIndex:index + 1];
}];
}
}];