使用coreData
生活随笔
收集整理的這篇文章主要介紹了
使用coreData
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、設計數據模型
2、創建持久化視圖和控制器
1 #import "BIDViewController.h" 2 #import "BIDAppDelegate.h" 3 4 static NSString * const kLineEntityName = @"Line"; 5 static NSString * const kLineNumberKey = @"lineNumber"; 6 static NSString * const kLineTextKey = @"lineText"; 7 8 @interface BIDViewController () 9 10 @property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *lineFields; 11 12 @end 13 14 @implementation BIDViewController 15 16 - (void)viewDidLoad 17 { 18 [super viewDidLoad];//獲取應用委托的引用,使用引用獲得創建托管對象上下文。 20 BIDAppDelegate * appDelegate = [UIApplication sharedApplication].delegate; 21 NSManagedObjectContext * context = [appDelegate managedObjectContext];
//創建一個獲取請求并將實體描述傳遞給它,以便請求知道要檢索的對象類型。 22 NSFetchRequest * request = [[NSFetchRequest alloc] 23 initWithEntityName:kLineEntityName];
24 //通過執行沒有謂語的請求,上下文將返回庫中的每一個Line對象。 25 NSError * error; 26 NSArray * objects = [context executeFetchRequest:request error:&error]; 27 if (objects == nil) { 28 NSLog(@"There was an error!"); 29 // Do whatever error handling is appropriate 30 } 31
//遍歷以獲取托管對象數組,從中提取每個托管對象的lineNumber和lineText值,并使用信息更新界面的文本框。 32 for (NSManagedObject * oneObject in objects) { 33 int lineNum = [[oneObject valueForKey:kLineNumberKey] intValue]; 34 NSString *lineText = [oneObject valueForKey:kLineTextKey]; 35 36 UITextField *theField = self.lineFields[lineNum]; 37 theField.text = lineText; 38 } 39 40 UIApplication *app = [UIApplication sharedApplication]; 41 [[NSNotificationCenter defaultCenter] 42 addObserver:self 43 selector:@selector(applicationWillResignActive:) 44 name:UIApplicationWillResignActiveNotification 45 object:app]; 46 } 47 48 - (void)applicationWillResignActive:(NSNotification *)notification 49 {
//先獲取對應的委托引用,然后使用此引用獲取指向應用的默認上下文指針。 50 BIDAppDelegate * appDelegate = [UIApplication sharedApplication].delegate; 51 NSManagedObjectContext * context = [appDelegate managedObjectContext]; 52 NSError *error; 53 for (int i = 0; i < 4; i++) { 54 UITextField *theField = self.lineFields[i];
//為line實體創建獲取請求,創建一個謂語,確認持久存儲中是否已經有一個與這個字段對應的托管對象。 56 NSFetchRequest *request = [[NSFetchRequest alloc] 57 initWithEntityName:kLineEntityName]; 58 NSPredicate *pred = [NSPredicate 59 predicateWithFormat:@"(%K = %d)", kLineNumberKey, i]; 60 [request setPredicate:pred]; 61 //在上下文中執行獲取請求 62 NSArray * objects = [context executeFetchRequest:request error:&error]; 63 if (objects == nil) { 64 NSLog(@"There was an error!"); 65 // Do whatever error handling is appropriate 66 }
67 //申明一個指向NSManagedObject的指針并將它設置為nil。檢查返回值對象objects,如果存在有效對象就加載,否則創建一個新的托管對象來保存這個字段的文本。 68 NSManagedObject * theLine = nil; 69 if ([objects count] > 0) { 70 theLine = [objects objectAtIndex:0]; 71 } else { 72 theLine = [NSEntityDescription 73 insertNewObjectForEntityForName:kLineEntityName 74 inManagedObjectContext:context]; 75 } 76 //使用鍵——值編碼來設置行號以及此托管對象的文本。 77 [theLine setValue:[NSNumber numberWithInt:i] forKey:kLineNumberKey]; 78 [theLine setValue:theField.text forKey:kLineTextKey]; 80 }
//通知上下文保存修改。 81 [appDelegate saveContext]; 82 }
?
轉載于:https://www.cnblogs.com/fengmin/p/5382587.html
總結
以上是生活随笔為你收集整理的使用coreData的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SAS数据挖掘实战篇【六】
- 下一篇: 2016去哪儿编程题:乘坐公交