· 添加一個Data Modelsql
· 添加 Entity , 點擊 "Add Entity"app
重命名爲"User"fetch
添加 Attributes : jid 和 password,類型都爲 string
spa
· 添加 NSManagedObjectcode
· 接下來能夠建立一個help類來幫助咱們使用sqlite
userCoreDataHelper.hip
#import <Foundation/Foundation.h> #import <CoreData/CoreData.h> #import "User.h" @interface userCoreDataHelper : NSObject{ NSManagedObjectContext *managedObjectContext; NSManagedObjectModel *managedObjectModel; NSPersistentStoreCoordinator *persistentStoreCoordinator; } // 保存信息 - (BOOL)saveMessageWithjid:(NSString *)jid password:(NSString *)password; // 獲取信息 - (NSArray *)getAllUsers; @end
userCoreDataHelper.mget
#import "userCoreDataHelper.h" @implementation userCoreDataHelper // 保存信息 - (BOOL)saveMessageWithjid:(NSString *)jid password:(NSString *)password{ BOOL result = NO; if ([jid length] == 0 || [password length] == 0){ NSLog(@"fromJID and toJID are mandatory."); return NO; } NSManagedObjectContext *moContext = [self managedObjectContext]; User *newUser = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:moContext]; if (newUser == nil){ NSLog(@"Failed to create the new message."); return NO; } newUser.jid = jid; newUser.password = password; NSError *savingError = nil; if ([moContext save:&savingError]){ return YES; }else{ NSLog(@"Failed to save the new message. Error = %@", savingError); } return result; } // 獲取信息 - (NSArray *)getAllUsers{ NSManagedObjectContext *context = [self managedObjectContext]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context]; [fetchRequest setEntity:entity]; NSError *error = nil; NSArray *fetchObject = [context executeFetchRequest:fetchRequest error:&error]; if(error!=nil){ return nil; } return fetchObject; } #pragma mark - Core Data stack // Returns the managed object context for the application. // If the context doesn't already exist, it is created and bound to thepersistent store coordinator for the application. - (NSManagedObjectContext *)managedObjectContext { if (managedObjectContext !=nil) { return managedObjectContext; } NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; if (coordinator != nil) { managedObjectContext = [[NSManagedObjectContext alloc]init]; [managedObjectContext setPersistentStoreCoordinator:coordinator]; } return managedObjectContext; } // Returns the managed object model for the application. // If the model doesn't already exist, it is created from the application'smodel. - (NSManagedObjectModel *)managedObjectModel { if (managedObjectModel !=nil) { return managedObjectModel; } // 注意,這裏的URLForResource要寫剛纔與使用的NSManagedObject一致的DataModal,不然會報錯。 NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"XMPPUsersModal_UserCoreData" withExtension:@"momd"]; managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; return managedObjectModel; } // Returns the persistent store coordinator for the application. // If the coordinator doesn't already exist, it is created and theapplication's store added to it. - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (persistentStoreCoordinator !=nil) { return persistentStoreCoordinator; } //這裏的 .sqlite,也應該與DataModal的名字保持一致。 NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"XMPPUsersModal_UserCoreData.sqlite"]; NSError *error = nil; persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { NSLog(@"Unresolvederror %@, %@", error, [error userInfo]); abort(); } return persistentStoreCoordinator; } #pragma mark - Application's Documents directory // Returns the URL to the application's Documents directory. - (NSURL*)applicationDocumentsDirectory { return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; } @end
使用string
在 Appdelegate 中添加:it
+ (userCoreDataHelper *)userCoreDataHelper{ return [[userCoreDataHelper alloc] init]; }
調用:
[[AppDelegate userCoreDataHelper] saveMessageWithjid:@"user1" password:@"pwd1"];
note: 添加了新的 Data Modal 和 NSManagedObject 後, 最好把原先模擬器上的程序移除再安裝。