Mhurd has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/102360


Change subject: Improved history core data concurrency.
......................................................................

Improved history core data concurrency.

Change-Id: I46d1a3bff139f4aee1e9d050fc73c5cc47337510
---
M Wikipedia-iOS.xcodeproj/project.pbxproj
M Wikipedia-iOS/View Controllers/History/HistoryViewController.m
M Wikipedia-iOS/View Controllers/SavedPages/SavedPagesViewController.m
3 files changed, 48 insertions(+), 22 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/ios/wikipedia 
refs/changes/60/102360/1

diff --git a/Wikipedia-iOS.xcodeproj/project.pbxproj 
b/Wikipedia-iOS.xcodeproj/project.pbxproj
index f5f279a..7718753 100644
--- a/Wikipedia-iOS.xcodeproj/project.pbxproj
+++ b/Wikipedia-iOS.xcodeproj/project.pbxproj
@@ -342,12 +342,12 @@
                04293002186030B0002A13FC /* SavedPages */ = {
                        isa = PBXGroup;
                        children = (
+                               0429300818604898002A13FC /* 
SavedPagesViewController.h */,
+                               0429300918604898002A13FC /* 
SavedPagesViewController.m */,
                                0429300318604898002A13FC /* 
SavedPagesResultCell.h */,
                                0429300418604898002A13FC /* 
SavedPagesResultCell.m */,
                                0429300618604898002A13FC /* 
SavedPagesTableHeadingLabel.h */,
                                0429300718604898002A13FC /* 
SavedPagesTableHeadingLabel.m */,
-                               0429300818604898002A13FC /* 
SavedPagesViewController.h */,
-                               0429300918604898002A13FC /* 
SavedPagesViewController.m */,
                                047E74131860509000916964 /* 
SavedPagesResultPrototypeView.xib */,
                        );
                        path = SavedPages;
diff --git a/Wikipedia-iOS/View Controllers/History/HistoryViewController.m 
b/Wikipedia-iOS/View Controllers/History/HistoryViewController.m
index 91446c2..45200de 100644
--- a/Wikipedia-iOS/View Controllers/History/HistoryViewController.m
+++ b/Wikipedia-iOS/View Controllers/History/HistoryViewController.m
@@ -105,6 +105,7 @@
     NSArray *historyEntities = [articleDataContext_.mainContext 
executeFetchRequest:fetchRequest error:&error];
     //XCTAssert(error == nil, @"Could not fetch.");
     for (History *history in historyEntities) {
+        /*
         NSLog(@"HISTORY:\n\t\
             article: %@\n\t\
             site: %@\n\t\
@@ -119,18 +120,19 @@
             history.discoveryMethod,
             history.article.thumbnailImage.fileName
         );
-        
+        */
+
         if ([history.dateVisited isToday]) {
-            [today addObject:history];
+            [today addObject:history.objectID];
         }else if ([history.dateVisited isYesterday]) {
-            [yesterday addObject:history];
+            [yesterday addObject:history.objectID];
         }else if ([history.dateVisited isLaterThanDate:[[NSDate date] 
dateBySubtractingDays:7]]) {
-            [lastWeek addObject:history];
+            [lastWeek addObject:history.objectID];
         }else if ([history.dateVisited isLaterThanDate:[[NSDate date] 
dateBySubtractingDays:30]]) {
-            [lastMonth addObject:history];
+            [lastMonth addObject:history.objectID];
         }else{
             // Older than 30 days == Garbage! Remove!
-            [garbage addObject:history];
+            [garbage addObject:history.objectID];
         }
     }
     
@@ -150,19 +152,33 @@
     //NSLog(@"GARBAGE = %@", garbage);
     if (garbage.count == 0) return;
 
-    for (History *history in garbage) {
-        // Article deletes don't cascade to images (intentionally) so delete 
these article thumbnails manually.
-        Image *thumb = history.article.thumbnailImage;
-        if (thumb) [articleDataContext_.mainContext deleteObject:thumb];
+    [articleDataContext_.workerContext performBlockAndWait:^(){
+        for (NSManagedObjectID *historyID in garbage) {
+            History *history = (History *)[articleDataContext_.workerContext 
objectWithID:historyID];
+            Article *article = history.article;
+            Image *thumb = history.article.thumbnailImage;
+            
+            // Delete the expired history record
+            [articleDataContext_.workerContext deleteObject:history];
 
-        // Image deletes don't cascade when article is deleted so delete 
manually for now.
-        Article *article = history.article;
-        if (article) [articleDataContext_.mainContext deleteObject:article];
-    }
-    
-    NSError *error = nil;
-    [articleDataContext_.mainContext save:&error];
-    //NSLog(@"GARBAGE error = %@", error);
+            BOOL isSaved = (article.saved.count > 0) ? YES : NO;
+
+            if (isSaved) continue;
+
+            // Article deletes don't cascade to images (intentionally) so 
delete these manually.
+            if (thumb) [articleDataContext_.workerContext deleteObject:thumb];
+
+//TODO: add code for deleting images which were only referenced by this article
+
+            // Delete the article
+            if (article) [articleDataContext_.workerContext 
deleteObject:article];
+
+        }
+        NSError *error = nil;
+        [articleDataContext_.workerContext save:&error];
+        if (error) NSLog(@"GARBAGE error = %@", error);
+
+    }];
 }
 
 #pragma mark - History section titles
@@ -233,7 +249,11 @@
     NSDictionary *dict = self.historyDataArray[indexPath.section];
     NSArray *array = [dict objectForKey:@"data"];
     
-    History *historyEntry = (History *)array[indexPath.row];
+    __block History *historyEntry = nil;
+    [articleDataContext_.mainContext performBlockAndWait:^(){
+        NSManagedObjectID *historyEntryId = (NSManagedObjectID 
*)array[indexPath.row];
+        historyEntry = (History *)[articleDataContext_.mainContext 
objectWithID:historyEntryId];
+    }];
     
     NSString *title = [historyEntry.article.title 
stringByReplacingOccurrencesOfString:@"_" withString:@" "];
     
@@ -277,7 +297,11 @@
     NSArray *array = dict[@"data"];
     selectedCell = array[indexPath.row];
     
-    History *historyEntry = (History *)array[indexPath.row];
+    __block History *historyEntry = nil;
+    [articleDataContext_.mainContext performBlockAndWait:^(){
+        NSManagedObjectID *historyEntryId = (NSManagedObjectID 
*)array[indexPath.row];
+        historyEntry = (History *)[articleDataContext_.mainContext 
objectWithID:historyEntryId];
+    }];
 
     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
     
diff --git a/Wikipedia-iOS/View 
Controllers/SavedPages/SavedPagesViewController.m b/Wikipedia-iOS/View 
Controllers/SavedPages/SavedPagesViewController.m
index b0b0c92..0b89137 100644
--- a/Wikipedia-iOS/View Controllers/SavedPages/SavedPagesViewController.m
+++ b/Wikipedia-iOS/View Controllers/SavedPages/SavedPagesViewController.m
@@ -96,6 +96,7 @@
     NSArray *savedPagesEntities = [articleDataContext_.mainContext 
executeFetchRequest:fetchRequest error:&error];
     //XCTAssert(error == nil, @"Could not fetch.");
     for (Saved *savedPage in savedPagesEntities) {
+        /*
         NSLog(@"SAVED:\n\t\
               article: %@\n\t\
               date: %@\n\t\
@@ -104,6 +105,7 @@
               savedPage.dateSaved,
               savedPage.article.thumbnailImage.fileName
               );
+        */
         [pages addObject:savedPage.objectID];
     }
     

-- 
To view, visit https://gerrit.wikimedia.org/r/102360
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I46d1a3bff139f4aee1e9d050fc73c5cc47337510
Gerrit-PatchSet: 1
Gerrit-Project: apps/ios/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Mhurd <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to