TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
Once a fetch, does query request as many times as you wants.
// fetch all -> filter by predicate
@interface RequestInMemory : NSObject
@property(nonatomic, strong) NSArray *fetchedContents;
// initialize class + fetch data from CoreData
+ (instancetype)memoryEntityDescription:(NSEntityDescription *) entityDescription context:(NSManagedObjectContext *) managedObjectContext;
+ (instancetype)memoryEntityDescription:(NSEntityDescription *) entityDescription predicate:(NSPredicate *) predicate context:(NSManagedObjectContext *) managedObjectContext;
#pragma mark - manually
// manually update internal database
// when initialize the class, automatically call this method.
- (NSArray *)fetchAll;
- (NSArray *)fetchWithPredicate:(NSPredicate *) predicate;
#pragma mark - filter helper
- (BOOL)testWithPredicate:(NSPredicate *) predicate;
- (NSArray *)findFirstWithPredicate:(NSPredicate *) predicate;
- (NSArray *)findAllWithPredicate:(NSPredicate *) predicate;
@end
First RequestInMemory
fetch ALL data only once.
You can does query request without having to access the CoreData next time.
You try to pod try RequestInMemory
- (void)viewDidAppear:(BOOL) animated {
[super viewDidAppear:animated];
CFTimeInterval startTime = CACurrentMediaTime();
{
for (size_t i = 0; i < 1000; i++) {
@autoreleasepool {
[self performCoreData];
}
}
}
CFTimeInterval endTime = CACurrentMediaTime();
NSLog(@"Total Runtime performCoreData: %g s", endTime - startTime);
CFTimeInterval startTime_b = CACurrentMediaTime();
{
self.personInMemory = [RequestInMemory memoryEntityDescription:[Person MR_entityDescription] context:[NSManagedObjectContext MR_defaultContext]];
for (size_t i = 0; i < 1000; i++) {
@autoreleasepool {
[self performRequestInMemory];
}
}
}
CFTimeInterval endTime_b = CACurrentMediaTime();
NSLog(@"Total Runtime performRequestInMemory: %g s", endTime_b - startTime_b);
}
- (void)performRequestInMemory {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %d", @"age", 10];
[self.personInMemory findFirstPredicate:predicate];
}
- (void)performCoreData {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %d", @"age", 10];
[Person MR_findFirstWithPredicate:predicate];
}
Result:
Total Runtime performCoreData: 1.53506 s
Total Runtime performRequestInMemory: 0.956278 s
RequestInMemory
fetch ALL data. In other words, RequestInMemory
is memory-hogging.
Not effective, but simple. (Welcome to Pull Requests! more effective way!)
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
MIT