Skip to content

Instantly share code, notes, and snippets.

@ccgus
Created February 4, 2012 22:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ccgus/1740787 to your computer and use it in GitHub Desktop.
Save ccgus/1740787 to your computer and use it in GitHub Desktop.
OLD:
FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
[db executeUpdate:@"insert into namedparamtest values (:a, :b, :c, :d)" withParameterDictionary:dictionaryArgs];
NEW:
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
[queue inDatabase:^(FMDatabase *db) {
[db executeUpdate:@"insert into namedparamtest values (:a, :b, :c, :d)" withParameterDictionary:dictionaryArgs];
}];
@ccgus
Copy link
Author

ccgus commented Feb 4, 2012

  • (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];
                }
            }
        }
    }];
    

    }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment