簡體   English   中英

在核心數據中存儲和獲取NSMutableArray

[英]Store and fetch NSMutableArray in Core Data

當我的應用程序終止/進入后台時,我想將NSMutableArray保存到Core-Data ,並且當我的應用程序啟動/變得活動時,我想加載NSMutableArray

我對Core-Data還沒有很好的了解。 這是我第一次使用它。 我看了很多視頻,教程,以前的Stackoverflow問題和Apple的文檔。 我認為我要嘗試做的事屬於Apple Core-Data文檔中的“非標准持久性屬性”一章。

我已經建立了一個名為TableViewList的實體,並給了它一個名為List of可轉換類型的屬性。

這是我的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);
}

結果數組返回一個空數組。 我認為我沒有正確保存和獲取數組。 在此先感謝您的幫助!

我使用此鏈接在對象中實現NSCoding

好,這里有幾件事要提到:

  1. applicationDidEnterBackground ,方法的前半部分配置您從未使用過的新托管對象上下文。 由於您從應用程序委托那里獲得了不同的托管對象上下文,因此不必在此處創建該對象,因此可以刪除創建新上下文的代碼。 您可能還想在applicationDidBecomeActive使用應用程序委托的上下文,盡管您所擁有的不一定是錯誤的。

  2. 您永遠不會保存更改。 您需要在托管對象上下文上調用save:將數據保存到持久性存儲文件中。

  3. 為了使用可轉換的屬性,您保存的數據必須符合NSCoding (因為Core Data不知道如何轉換任意類,而NSCoding就是您告訴它要做什么)。 NSArray確實可以,但是數組中的所有內容也必須保持一致也很重要。 如果您的自定義類做到了,那就可以了。 如果不是,則需要修復該問題以保存陣列或找到其他方法來保存數據。

  4. 無論您做什么,我都不相信您會得到可變的數組。 保存並獲取工作后,您將獲得一個不可變的數組作為list屬性的值。 因此,如果需要使數組可變,則需要調用mutableCopy

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM