diff --git a/Sources/Controllers/MyPlaces/OAFavoriteListViewController.mm b/Sources/Controllers/MyPlaces/OAFavoriteListViewController.mm index 4093ab2375..61e108295e 100644 --- a/Sources/Controllers/MyPlaces/OAFavoriteListViewController.mm +++ b/Sources/Controllers/MyPlaces/OAFavoriteListViewController.mm @@ -717,7 +717,7 @@ - (void)colorChanged } } } - [OAFavoritesHelper saveCurrentPointsIntoFile]; + [OAFavoritesHelper saveCurrentPointsIntoFile:YES]; } [self finishEditing]; [self.favoriteTableView reloadData]; diff --git a/Sources/Controllers/TargetMenu/PointEditing/OAFavoriteGroupEditorViewController.mm b/Sources/Controllers/TargetMenu/PointEditing/OAFavoriteGroupEditorViewController.mm index 30045a9dbe..c29f17eaf0 100644 --- a/Sources/Controllers/TargetMenu/PointEditing/OAFavoriteGroupEditorViewController.mm +++ b/Sources/Controllers/TargetMenu/PointEditing/OAFavoriteGroupEditorViewController.mm @@ -109,7 +109,7 @@ - (void)editPointsGroup:(BOOL)updatePoints newName:self.editName saveImmediately:NO]; - [OAFavoritesHelper saveCurrentPointsIntoFile]; + [OAFavoritesHelper saveCurrentPointsIntoFile:YES]; if (self.delegate) [self.delegate onEditorUpdated]; [self dismissViewController]; diff --git a/Sources/Helpers/OAFavoritesHelper.h b/Sources/Helpers/OAFavoritesHelper.h index 1e09f47062..70e674f922 100644 --- a/Sources/Helpers/OAFavoritesHelper.h +++ b/Sources/Helpers/OAFavoritesHelper.h @@ -48,7 +48,7 @@ + (BOOL) editFavoriteName:(OAFavoriteItem *)item newName:(NSString *)newName group:(NSString *)group descr:(NSString *)descr address:(NSString *)address; + (BOOL) editFavorite:(OAFavoriteItem *)item lat:(double)lat lon:(double)lon; + (BOOL) editFavorite:(OAFavoriteItem *)item lat:(double)lat lon:(double)lon description:(NSString *)description; -+ (void) saveCurrentPointsIntoFile; ++ (void) saveCurrentPointsIntoFile:(BOOL)async; + (void)updateGroup:(OAFavoriteGroup *)group newName:(NSString *)newName diff --git a/Sources/Helpers/OAFavoritesHelper.mm b/Sources/Helpers/OAFavoritesHelper.mm index f8508fc875..beffbca29c 100644 --- a/Sources/Helpers/OAFavoritesHelper.mm +++ b/Sources/Helpers/OAFavoritesHelper.mm @@ -36,6 +36,9 @@ #define BACKUP_MAX_COUNT 10 #define BACKUP_MAX_PER_DAY 3 +static NSObject *_syncObj = [[NSObject alloc] init]; +static dispatch_queue_t _favQueue = dispatch_queue_create("favorite_helper_queue", DISPATCH_QUEUE_SERIAL); + @implementation OAFavoritesHelper static OAObservable *_favoritesCollectionChangedObservable; @@ -339,7 +342,7 @@ + (BOOL)addFavorites:(NSArray *)favorites if (sortAndSave) { [self sortAll]; - [self saveCurrentPointsIntoFile]; + [self saveCurrentPointsIntoFile:YES]; } } return res; @@ -426,7 +429,7 @@ + (BOOL) editFavoriteName:(OAFavoriteItem *)item newName:(NSString *)newName gro [appearanceCollection selectColor:[appearanceCollection getColorItemWithValue:[[item getColor] toARGBNumber]]]; [self sortAll]; - [self saveCurrentPointsIntoFile]; + [self saveCurrentPointsIntoFile:YES]; return YES; } @@ -445,7 +448,7 @@ + (BOOL) editFavorite:(OAFavoriteItem *)item lat:(double)lat lon:(double)lon des if (description) [item setDescription:description]; - [self saveCurrentPointsIntoFile]; + [self saveCurrentPointsIntoFile:YES]; return YES; } @@ -477,7 +480,7 @@ + (void)updateGroup:(OAFavoriteGroup *)group [renamedGroup.points addObjectsFromArray:group.points]; } if (saveImmediately) - [self saveCurrentPointsIntoFile]; + [self saveCurrentPointsIntoFile:YES]; // TODO: false in Android } + (void)updateGroup:(OAFavoriteGroup *)group @@ -494,7 +497,7 @@ + (void)updateGroup:(OAFavoriteGroup *)group } group.iconName = iconName; if (saveImmediately) - [self saveCurrentPointsIntoFile]; + [self saveCurrentPointsIntoFile:YES]; // TODO: false in Android } + (void)updateGroup:(OAFavoriteGroup *)group @@ -511,7 +514,7 @@ + (void)updateGroup:(OAFavoriteGroup *)group } group.color = color; if (saveImmediately) - [self saveCurrentPointsIntoFile]; + [self saveCurrentPointsIntoFile:YES]; // TODO: false in Android } + (void)updateGroup:(OAFavoriteGroup *)group @@ -528,15 +531,54 @@ + (void)updateGroup:(OAFavoriteGroup *)group } group.backgroundType = backgroundIconName; if (saveImmediately) - [self saveCurrentPointsIntoFile]; + [self saveCurrentPointsIntoFile:YES]; // TODO: false in Android +} + ++ (void) saveCurrentPointsIntoFile:(BOOL)async +{ + if (async) + [self.class saveFavoritesIntoFile]; + else + [self.class saveFavoritesIntoFileSync]; +} + ++ (void) saveFavoritesIntoFile +{ + NSLog(@"!!! saveFavoritesIntoFile Async 0 %@ %@", _syncObj, _favQueue); + @synchronized (_syncObj) + { + NSLog(@"!!! saveFavoritesIntoFile Async 1 %@ %@", _syncObj, _favQueue); + dispatch_async(_favQueue, ^{ +// dispatch_async(dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ +// dispatch_async(dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ +// dispatch_async(dispatch_get_main_queue(), ^{ + NSLog(@"!!! saveFavoritesIntoFile Async 2 %@ %@", _syncObj, _favQueue); + [self.class saveCurrentPointsIntoFile]; +// [NSThread sleepForTimeInterval:15.0f]; + }); + } } ++ (void) saveFavoritesIntoFileSync +{ + NSLog(@"!!! saveFavoritesIntoFile Sync"); + [self.class saveCurrentPointsIntoFile]; +} + + (void) saveCurrentPointsIntoFile { + NSDate *start = [NSDate now]; + + NSMutableArray *favoriteGroups = [NSMutableArray arrayWithArray:_favoriteGroups]; + + NSLog(@"< !!! saveCurrentPointsIntoFile 0 start %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); + NSMutableDictionary *deletedGroups = [NSMutableDictionary dictionary]; NSMutableDictionary *deletedPoints = [NSMutableDictionary dictionary]; NSArray *files = [self getGroupFiles]; + NSLog(@" !!! saveCurrentPointsIntoFile 1 getGroupFiles %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); + if (files.count > 0) { for (NSString *file in files) @@ -544,6 +586,8 @@ + (void) saveCurrentPointsIntoFile [self loadFileGroups:file groups:deletedGroups]; } } + NSLog(@" !!! saveCurrentPointsIntoFile 2 loadFileGroups %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); + // Get all points from internal file to filter later for (OAFavoriteGroup *group in deletedGroups.allValues) { @@ -552,22 +596,31 @@ + (void) saveCurrentPointsIntoFile deletedPoints[[point getKey]] = point; } } + NSLog(@" !!! saveCurrentPointsIntoFile 3 deletedPoints %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); + // Hold only deleted points in map - for (OAFavoriteItem *point in [self getPointsFromGroups:_favoriteGroups]) + for (OAFavoriteItem *point in [self getPointsFromGroups:favoriteGroups]) { [deletedPoints removeObjectForKey:[point getKey]]; } + NSLog(@" !!! saveCurrentPointsIntoFile 4 deletedPoints %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); + // Hold only deleted groups in map - for (OAFavoriteGroup *group in _favoriteGroups) + for (OAFavoriteGroup *group in favoriteGroups) { [deletedGroups removeObjectForKey:group.name]; } + NSLog(@" !!! saveCurrentPointsIntoFile 5 deletedGroups %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); + // Save groups to internal file // [self saveFile:_favoriteGroups file:internalFile]; // Save groups to external files - [self saveFiles:_favoriteGroups deleted:deletedPoints.allKeys]; + [self saveFiles:favoriteGroups deleted:deletedPoints.allKeys]; + NSLog(@" !!! saveCurrentPointsIntoFile 6 saveFiles %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); + // Save groups to backup file [self backup]; + NSLog(@"> !!! saveCurrentPointsIntoFile 7 backup %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); } + (NSArray *)getPointsFromGroups:(NSArray *)groups @@ -583,9 +636,13 @@ + (void) saveCurrentPointsIntoFile + (void)saveFiles:(NSArray *)localGroups deleted:(NSArray *)deleted { + NSDate *start = [NSDate now]; NSDictionary *fileGroups = [self loadGroups]; + NSLog(@" < !!! saveFiles 1 loadGroups %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); [self saveFileGroups:localGroups fileGroups:fileGroups]; + NSLog(@" !!! saveFiles 2 saveFileGroups %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); [self saveLocalGroups:localGroups fileGroups:fileGroups deleted:deleted]; + NSLog(@" > !!! saveFiles 3 saveLocalGroups %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); } + (void)saveFileGroups:(NSArray *)localGroups @@ -622,9 +679,13 @@ + (void)saveLocalGroups:(NSArray *)localGroups fileGroups:(NSDictionary *)fileGroups deleted:(NSArray *)deleted { + NSDate *start = [NSDate now]; + NSLog(@" < !!! saveLocalGroups 1 start %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); for (OAFavoriteGroup *localGroup in localGroups) { OAFavoriteGroup *fileGroup = fileGroups[localGroup.name]; + NSLog(@" *all = [NSMutableDictionary dictionary]; if (fileGroup) @@ -635,15 +696,21 @@ + (void)saveLocalGroups:(NSArray *)localGroups if (![deleted containsObject:key]) all[key] = point; } + NSLog(@" !!! saveLocalGroups 3 if(fileGroup) %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); } + // Remove already existing in memory NSArray *localPoints = localGroup.points; for (OAFavoriteItem *point in localPoints) { [all removeObjectForKey:[point getKey]]; } + NSLog(@" !!! saveLocalGroups 4 localPoints %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); + // save favoritePoints from memory in order to update existing [localGroup.points addObjectsFromArray:all.allValues]; + NSLog(@" !!! saveLocalGroups 5 addObjectsFromArray %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); + // Save file if group changed if (![localGroup isEqual:fileGroup]) { @@ -654,15 +721,22 @@ + (void)saveLocalGroups:(NSArray *)localGroups localGroup.name.length > 0 ? app.favoritesGroupNameSeparator : @"", localGroup.name, GPX_FILE_EXT]]; + NSLog(@" !!! saveLocalGroups 6 fileGroupPath %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); + [self saveFile:@[localGroup] file:fileGroupPath]; + NSLog(@" !!! saveLocalGroups 7 saveFile %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); } + NSLog(@" >!!! saveLocalGroups 8 for %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); } } + (void)saveFile:(NSArray *)favoriteGroups file:(NSString *)file { + NSDate *start = [NSDate now]; OAGPXMutableDocument *gpx = [self asGpxFile:favoriteGroups]; + NSLog(@" < !!! saveFile 1 asGpxFile %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); [gpx saveTo:file]; + NSLog(@" > !!! saveFile 1 saveTo %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); } + (void) backup @@ -877,7 +951,7 @@ + (void)deleteFavorites:(NSArray *)favorites saveImmediately:( } [self removeFavoritePoints:favorites favoriteLocations:favoriteLocations]; if (saveImmediately) - [self saveCurrentPointsIntoFile]; + [self saveCurrentPointsIntoFile:YES]; //TODO: false in Android } + (void)removeFavoritePoints:(NSArray *)favorites favoriteLocations:(const QList< std::shared_ptr > &)favoriteLocations @@ -921,7 +995,7 @@ + (BOOL) deleteFavoriteGroups:(NSArray *)groupsToDelete andFa } } if (!isNewFavorite) - [self saveCurrentPointsIntoFile]; + [self saveCurrentPointsIntoFile:YES]; //TODO: false in Android return YES; } @@ -971,11 +1045,18 @@ + (BOOL) deleteFavoriteGroups:(NSArray *)groupsToDelete andFa + (OAGPXMutableDocument *) asGpxFile:(NSArray *)favoriteGroups { + NSDate *start = [NSDate now]; + NSLog(@" < !!! asGpxFile start %2f %d", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970, favoriteGroups != nil); OAGPXMutableDocument *gpx = [[OAGPXMutableDocument alloc] init]; for (OAFavoriteGroup *group in favoriteGroups) { - [gpx addPointsGroup:[group toPointsGroup]]; + NSLog(@" !!! asGpxFile for start %2f %d", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970, group != nil); + OAPointsGroup * foo = [group toPointsGroup]; + NSLog(@" !!! asGpxFile toPointsGroup %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); + [gpx addPointsGroup:foo]; + NSLog(@" !!! asGpxFile addPointsGroup %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); } + NSLog(@" > !!! asGpxFile end %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); return gpx; } @@ -1218,21 +1299,32 @@ + (NSString *) convertDisplayNameToGroupIdName:(NSString *)name - (OAPointsGroup *)toPointsGroup { + NSDate *start = [NSDate now]; + NSLog(@" < !!! toPointsGroup 1 start %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); OAPointsGroup *pointsGroup = [[OAPointsGroup alloc] initWithName:_name iconName:_iconName backgroundType:_backgroundType color:_color]; + NSLog(@" !!! toPointsGroup 2 pointsGroup %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); + NSMutableArray *points = [NSMutableArray array]; for (OAFavoriteItem *point in _points) { - [points addObject:[point toWpt]]; +// NSLog(@" !!! toPointsGroup 30 for start %2f %d", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970, _points != nil); + OAWptPt *foo = [point toWpt]; +// NSLog(@" !!! toPointsGroup 31 toWpt %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); + [points addObject:foo]; +// NSLog(@" !!! toPointsGroup 32 addObject %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); } + NSLog(@" !!! toPointsGroup 34 for end %2f %d", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970, points != nil); pointsGroup.points = points; + NSLog(@" !!! toPointsGroup 3 points %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); std::shared_ptr pg; pg.reset(new OsmAnd::GpxDocument::PointsGroup()); [OAGPXDocument fillPointsGroup:pg usingPointsGroup:pointsGroup]; pointsGroup.pg = pg; + NSLog(@" > !!! toPointsGroup 4 fillPointsGroup %2f", [NSDate now].timeIntervalSince1970 - start.timeIntervalSince1970); return pointsGroup; }