Skip to content

Commit

Permalink
Add convenient method to clean all loaded (cached) instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Kjuly committed Jul 10, 2016
1 parent 100a4b0 commit eb906b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions FCModel/FCModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ extern NSString * const FCModelChangedFieldsKey;
// All instances of the called class in memory. Call on a subclass, not FCModel directly. You probably don't need this, until you do.
+ (NSArray *)allLoadedInstances;

// Convenient method to clean all loaded (cached) instances. Call on a subclass to clean for the specific table; Call on FCModel directly to clean for all tables.
+ (void)cleanAllLoadedInstances;

// Issues SQLite VACUUM to rebuild database and recover deleted pages. Returns NO if a transaction is in progress that prevents it.
+ (BOOL)vacuumIfPossible;

Expand Down
8 changes: 8 additions & 0 deletions FCModel/FCModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ + (NSArray *)allLoadedInstances
return outArray ?: @[];
}

+ (void)cleanAllLoadedInstances
{
fcm_onMainThread(^{
if (self.class == FCModel.class) [g_instances removeAllObjects];
else [g_instances removeObjectForKey:self];
});
}

+ (instancetype)instanceWithPrimaryKey:(id)primaryKeyValue { return [self instanceWithPrimaryKey:primaryKeyValue databaseRowValues:nil createIfNonexistent:YES]; }
+ (instancetype)instanceWithPrimaryKey:(id)primaryKeyValue createIfNonexistent:(BOOL)create { return [self instanceWithPrimaryKey:primaryKeyValue databaseRowValues:nil createIfNonexistent:create]; }

Expand Down

0 comments on commit eb906b8

Please sign in to comment.