Background tasking unreading of stories.

This commit is contained in:
Samuel Clay 2014-03-05 21:31:53 -08:00
parent 83dcf5640b
commit 9b4c916883
2 changed files with 34 additions and 26 deletions

View file

@ -362,17 +362,22 @@
NSString *storyFeedId = [request.userInfo objectForKey:@"story_feed_id"];
NSString *storyHash = [request.userInfo objectForKey:@"story_hash"];
BOOL dequeued = [appDelegate dequeueReadStoryHash:storyHash inFeed:storyFeedId];
if (!dequeued) {
// Offline means can't unread a story unless it was read while offline.
[self markStoryRead:storyHash feedId:storyFeedId];
// [self.storyTitlesTable reloadData];
[appDelegate failedMarkAsUnread:request];
} else {
// Offline but read story while offline, so it never touched the server.
[appDelegate.unreadStoryHashes setObject:[NSNumber numberWithBool:YES] forKey:storyHash];
// [self.storyTitlesTable reloadData];
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
(unsigned long)NULL), ^(void) {
BOOL dequeued = [appDelegate dequeueReadStoryHash:storyHash inFeed:storyFeedId];
dispatch_async(dispatch_get_main_queue(), ^{
if (!dequeued) {
// Offline means can't unread a story unless it was read while offline.
[self markStoryRead:storyHash feedId:storyFeedId];
// [self.storyTitlesTable reloadData];
[appDelegate failedMarkAsUnread:request];
} else {
// Offline but read story while offline, so it never touched the server.
[appDelegate.unreadStoryHashes setObject:[NSNumber numberWithBool:YES] forKey:storyHash];
// [self.storyTitlesTable reloadData];
}
});
});
}
#pragma mark - Story Actions
@ -551,20 +556,23 @@
if (!newUnreadCounts) return;
[appDelegate.dictUnreadCounts setObject:newUnreadCounts forKey:feedIdStr];
[appDelegate.database inTransaction:^(FMDatabase *db, BOOL *rollback) {
NSString *storyHash = [newStory objectForKey:@"story_hash"];
[db executeUpdate:@"UPDATE stories SET story_json = ? WHERE story_hash = ?",
[newStory JSONRepresentation],
storyHash];
[db executeUpdate:@"INSERT INTO unread_hashes "
"(story_hash, story_feed_id, story_timestamp) VALUES (?, ?, ?)",
storyHash, feedIdStr, [newStory objectForKey:@"story_timestamp"]];
[db executeUpdate:@"UPDATE unread_counts SET ps = ?, nt = ?, ng = ? WHERE feed_id = ?",
[newUnreadCounts objectForKey:@"ps"],
[newUnreadCounts objectForKey:@"nt"],
[newUnreadCounts objectForKey:@"ng"],
feedIdStr];
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
(unsigned long)NULL), ^(void) {
[appDelegate.database inTransaction:^(FMDatabase *db, BOOL *rollback) {
NSString *storyHash = [newStory objectForKey:@"story_hash"];
[db executeUpdate:@"UPDATE stories SET story_json = ? WHERE story_hash = ?",
[newStory JSONRepresentation],
storyHash];
[db executeUpdate:@"INSERT INTO unread_hashes "
"(story_hash, story_feed_id, story_timestamp) VALUES (?, ?, ?)",
storyHash, feedIdStr, [newStory objectForKey:@"story_timestamp"]];
[db executeUpdate:@"UPDATE unread_counts SET ps = ?, nt = ?, ng = ? WHERE feed_id = ?",
[newUnreadCounts objectForKey:@"ps"],
[newUnreadCounts objectForKey:@"nt"],
[newUnreadCounts objectForKey:@"ng"],
feedIdStr];
}];
});
[appDelegate.recentlyReadStories removeObjectForKey:[story objectForKey:@"story_hash"]];
[appDelegate finishMarkAsUnread:story];

View file

@ -5,7 +5,7 @@
#import <SystemConfiguration/SystemConfiguration.h>
#import <MobileCoreServices/MobileCoreServices.h>
#define DEBUG 1
//#define DEBUG 1
//#define PROD_DEBUG 1
#ifdef DEBUG