當我的應用程序終止/進入後臺時,我想將NSMutableArray
保存爲Core-Data
,我想在應用程序啓動/激活時加載NSMutableArray
。我不太瞭解Core-Data
。這是我第一次使用它。我看了一大堆視頻,教程,之前的Stackoverflow問題和Apple的文檔。我認爲我想要做的是屬於Apple Core-Data
文檔中的Non-Standard Persistent Attributes章節。在覈心數據中存儲和提取NSMutableArray
我已經建立了一個名爲TableViewList的實體,並給它一個名爲List可變形的屬性。
這是我的AppDelegate.h和.m代碼。所有的建議都會很棒。
AppDelegate.h#import <UIKit/UIKit.h>
#import "TableViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property(nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property(nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property(nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
-(NSString *) applicationDocumentsDirectory;
@end
AppDelegate.m
#import <CoreData/CoreData.h>
#import "AppDelegate.h"
#import <UIKit/UIKit.h>
@interface AppDelegate()
@end
@implementation AppDelegate
@synthesize managedObjectModel;
@synthesize managedObjectContext;
@synthesize persistentStoreCoordinator;
- (void)applicationDidEnterBackground:(UIApplication *)application {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact;
newContact = [NSEntityDescription insertNewObjectForEntityForName:@"TableViewList" inManagedObjectContext:context];
NSData *arrayData = [NSKeyedArchiver archivedDataWithRootObject:ListArray];
[newContact setValue:arrayData forKey:@"list"];
NSError *error = nil;
[context save:&error];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:[NSBundle allBundles]];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
NSURL *url = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent: @"App1.sqlite"];
[coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:nil];
managedObjectContext = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
managedObjectContext.persistentStoreCoordinator = coordinator;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"TableViewList" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSArray *result = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (error) {
NSLog(@"unable to execute fetch request");
NSLog(@"%@, %@", error, error.localizedDescription);
}
else
NSLog(@"%@",result);
}
結果陣列返回一個空數組。我不認爲我正在保存並正確提取陣列。在此先感謝您的幫助!
我用this link在我的對象中實現了NSCoding
。
有兩個問題:1.數據模型中'list'屬性的類型是什麼? 2.您正在保存的陣列中有哪些對象? –
@TomHarrington 1.'list'是類型可轉換的。 2.該對象是一個具有三個屬性的自定義NSObject。兩個是NSString,一個是NSUInteger。 – njyulan
您確定要/需要使用Transformable嗎?您似乎將列表存儲爲單個對象上的單個屬性。您可以改爲擁有兩個字符串屬性和一個整數的實體,併爲數組中的每個對象另存一個該實體的實例。 – pbasdf