CoreData 手動自動 建立context(上下文)
方法一:自定義context
上下文關連資料庫,model模型檔案
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
持久化,把資料儲存到一個檔案,而不是記憶體
NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
告訴Coredata資料庫的名字和路徑
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *sqlitePath = [doc stringByAppendingPathComponent:@"YQHItList.sqlite"];
[store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlitePath] options:nil error:nil];
_context.persistentStoreCoordinator = store;
方法二:系統自動生成(在AppDelegate內部)然後其他類裡面進行呼叫:
怎麼 取到context?
AppDelegate*appDelegate=[[UIApplication sharedApplication]delegate];
self.context=appDelegate.managedObjectContext;
因為下面程式也要使用到
資料儲存
什麼時候進行資料儲存,要根據你自己的程式來
//確認按鈕
UIAlertAction *okAction=[UIAlertAction actionWithTitle:@"確認新增" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//例項化實體類
YQPerson *person =[NSEntityDescription insertNewObjectForEntityForName:@"YQPerson" inManagedObjectContext:_context ];
// 給實體類的name賦值
person.name=[alert.textFields[0] text];
NSError*error=nil;
[_context save:&error];
NSLog(@"%@",person.name);
//判斷如果出錯
if (error) {
NSLog(@"%@",person.name);
}
//將資料降入可變陣列
[_mutable addObject:person.name];
//重新整理資料(全域性重新整理)
[self.tableView reloadData];
}];
這裡我是在點選一個按鈕彈出alert框內部可以輸入text,在我點選確定按鈕的時候進行資料儲存
資料查詢(從你儲存的裡面取出來,下一次執行程式時還能顯示在螢幕上)
請求
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"YQPerson"];
NSError *error = nil;
從coredata中查詢出資料 存入一個陣列
NSArray *ps= [_context executeFetchRequest:request error:&error];
;
NSLog(@"%@",[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"YQHItList.sqlite"]);
NSLog(@"%@",ps);
if (error) {
NSLog(@"error");
}
_mutable = [NSMutableArray array];
注意二:這一步不能少,少了之後系統不能識別我們這個是什麼東西 ,必須借用一個新物件然後等價於我們這個陣列才可以進行下一步操作
[_mutable removeAllObjects];
快速查詢對應name 存入可變陣列用於介面顯示
for (YQPerson *p in ps) {
[_mutable addObject:p.name];
}
NSLog(@"%@",_mutable);
方法二怎麼自動生成的步驟就不提了 ,可以去其他帖子看一下,我這裡只是簡單做一下筆記,因為我之前做這個簡單的專案的時候遇到了比較難發現的問題: 能存進去但是取不出來
能存context save: 是成功的,但是fetch的時候就不成功了...
1.viewDidLoad中我[self fetch];操作放在了context獲取操作之前了 ,這樣就不能根據context 來獲取你儲存的東西了 記住
相關文章
- go 上下文:context.ContextGoContext
- React的上下文-ContextReactContext
- Spring AOP 自動建立代理Spring
- Golang context (上下文)是什麼GolangContext
- CSS 層疊上下文(Stacking Context)CSSContext
- odoo context上下文用法總結OdooContext
- idea自動建立實體類Idea
- 探祕 flex 上下文中神奇的自動 marginFlex
- 自己手動建立https證書HTTP
- 如何建立stacking context?Context
- iris 路由註冊和上下文context路由Context
- 胡塞爾現象=上下文ContextContext
- Python - Context Manager 上下文管理器PythonContext
- 上下文 Context 與結構體 StructContext結構體Struct
- SpringBoot(04)——建立自己的自動配置Spring Boot
- 獲取管理中心上下文ContextContext
- Sqlserver關於統計資訊自動建立自動更新的知識點SQLServer
- 禁用Windows自動更新並允許手動更新Windows
- drf☞jwt自動簽發與手動簽發JWT
- 使用fastlane match自動和手動管理證書AST
- mongodb怎麼手動建立資料庫MongoDB資料庫
- SpringBoot手動部署到Tomcat和自動化熱部署Spring BootTomcat熱部署
- 小程式從手動埋點到自動埋點
- 建立屬於自己的 Spring Boot 自動配置Spring Boot
- 自動為新建的表建立同義詞
- 介面自動化程式碼-AI 建立嘗試AI
- 函式計算自動化運維實戰3 -- 事件觸發自動建立快照函式運維事件
- oracle之 手動建立 emp 表 與 dept 表Oracle
- [轉] MySQL binlog 日誌自動清理及手動刪除MySql
- 自動擋換手動擋:在 ASP.NET Core 3.0 Middleware 中手動執行 Controller ActionASP.NETController
- 一起學context(一)——上下文值傳遞Context
- with open() as 的用法 和 with上下文管理器(Context manager)Context
- 什麼是Python中Context上下文管理器PythonContext
- iOS CoreDataiOS
- 手機「自動駕駛」大揭秘!vivo萬字綜述探討大模型手機自動化自動駕駛大模型
- 2.5.5 使用自動Undo管理: 建立 Undo 表空間
- Dependabot:自動建立GitHub PR修復潛在漏洞Github
- selenium模組,web自動化,建立瀏覽器Web瀏覽器
- 測者的測試技術手冊:自動的自動化EvoSuite 自動生成JUnit的測試用例UI