Skip to content

Instantly share code, notes, and snippets.

@ccgus
Created February 4, 2012 22:55
Show Gist options
  • Save ccgus/1740870 to your computer and use it in GitHub Desktop.
Save ccgus/1740870 to your computer and use it in GitHub Desktop.
- (void) deleteTweetsOlderThan:(int)inMaxToKeep
{
NSArray* recent_ids = [RFTweet tweetIDsReversedForCollectionID:self.collectionID];
if ([recent_ids count] > inMaxToKeep) {
FMDatabaseQueue *queue = …
[queue inDatabase:^(FMDatabase *db) {
if (inMaxToKeep > 0) {
NSNumber* last_tweet_id = [recent_ids objectAtIndex:inMaxToKeep - 1];
[db executeUpdate:@"DELETE FROM mappings WHERE tweet_id < ? AND collection_id = ?", last_tweet_id, self.collectionID];
}
else {
[db executeUpdate:@"DELETE FROM mappings WHERE collection_id = ?", self.collectionID];
}
};
[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
for (int i = inMaxToKeep; i < [recent_ids count]; i++) {
@autoreleasepool {
NSNumber* tweet_id_to_delete = [recent_ids objectAtIndex:i];
if (![RFMapping findWithColumn:@"tweet_id" value:tweet_id_to_delete accountID:self.accountID]) {
[db executeUpdate:@"DELETE FROM tweets WHERE id = ?", tweet_id_to_delete];
}
}
}
}];
}
}
or you could use inSavePoint:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment