Created
February 26, 2014 02:28
-
-
Save keicoder/9222374 to your computer and use it in GitHub Desktop.
objective-c : using coredata helper class and fetchRequest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//using coredata helper class and fetchRequest | |
//appDelegate의 NSManagedObjectContext 참조 | |
//TWCoreDataHelper.h | |
@interface TWCoreDataHelper : NSObject | |
+ (NSManagedObjectContext *)managedObjectContext; | |
@end | |
//TWCoreDataHelper.m | |
#import "TWCoreDataHelper.h" | |
@implementation TWCoreDataHelper | |
+ (NSManagedObjectContext *)managedObjectContext | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
NSManagedObjectContext *context = nil; | |
id delegate = [[UIApplication sharedApplication] delegate]; | |
if ([delegate performSelector:@selector(managedObjectContext)]) { | |
context = [delegate managedObjectContext]; | |
} | |
return context; | |
} | |
@end | |
//TWAlbumTableViewController.m | |
#import "TWCoreDataHelper.h" //코어데이터 헬퍼 클래스 | |
@implementation TWAlbumTableViewController | |
- (void)viewWillAppear:(BOOL)animated | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
[super viewWillAppear:animated]; | |
//코어데이터 데이터 가져와서 테이블 뷰에 부리기 | |
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Album"]; //give me all the Albums | |
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO]]; //arrange from newest to oldest | |
// //코어데이터 헬퍼 클래스에서 담당 | |
// id delegate = [[UIApplication sharedApplication] delegate]; | |
// NSManagedObjectContext *context = [delegate managedObjectContext]; | |
NSError *error = nil; | |
NSArray *fetchedAlbums = [[TWCoreDataHelper managedObjectContext] executeFetchRequest:fetchRequest | |
error:&error]; | |
self.albums = [fetchedAlbums mutableCopy]; //뮤터블 카피 | |
[self.tableView reloadData]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment