diff --git a/FCModel/FCModel.h b/FCModel/FCModel.h index 8c50574..9771d3d 100644 --- a/FCModel/FCModel.h +++ b/FCModel/FCModel.h @@ -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; diff --git a/FCModel/FCModel.m b/FCModel/FCModel.m index f878789..dc16017 100644 --- a/FCModel/FCModel.m +++ b/FCModel/FCModel.m @@ -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]; }