所以我有一個單身擁有一個NSArray。這個數組是從核心數據獲取請求中的120K +對象開始的。當我通過數組訪問我的視圖控制器中的這些對象時,它們是空的。它顯示了正確的對象數量,並且它們在初始化後在單例中顯示得很好。有什麼想法嗎?感謝您的幫助。核心數據對象的NSArray單身null
Singleton.h
@interface Singleton : NSObject
@property (nonatomic, retain) NSArray *singletonArray;
+(Singleton*)singleton;
@end
Singleton.m
#import "Singleton.h"
#import "DataAccessLayer.h"
@implementation Singleton
@synthesize singletonArray;
+(Singleton *)singleton {
static dispatch_once_t pred;
static __strong Singleton *shared = nil;
dispatch_once(&pred, ^{
shared = [[Singleton alloc]init];
});
return shared;
}
-(id)init
{
if (self = [super init])
{
DataAccessLayer *dataAccess = [[DataAccessLayer alloc]init];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext: dataAccess.managedObjectContext]];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"Name" ascending:YES]]];
NSError *error = nil;
self.singletonArray = [dataAccess.managedObjectContext executeFetchRequest:fetchRequest error:&error];
}
return self;
}
@end
ViewController.m
#import "ViewController.h"
#import "Singleton.h"
@interface ViewController()
@end
- (void)viewDidLoad
{
[super viewDidLoad];
FAKFontAwesome *settingsIcon = [FAKFontAwesome gearIconWithSize:22];
[settingsIcon addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor]];
UIImage *settingsIconImage = [settingsIcon imageWithSize:CGSizeMake(22, 22)];
UIButton *settingsIconButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
[settingsIconButton addTarget:self action:@selector(settingsBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[settingsIconButton setImage:settingsIconImage forState:UIControlStateNormal];
[self.settingsBarButton setCustomView:settingsIconButton];
self.masterArray = [NSArray new];
self.masterArray = [[Singleton singleton] singletonArray];
NSString *title = [[self.masterArray objectAtIndex:0] field1];
NSLog(@"Viewdidload: %@", title);
}
- (IBAction)settingsBarButtonClicked:(id)sender
{
NSString *title = [[self.masterArray objectAtIndex:0] field1];
NSLog(@"SettingsBarButtonClicked Log %@", title);
}
@end
控制檯輸出在辛格爾頓
(lldb) po [self.singletonArray count]
120441
(lldb) po [self.singletonArray objectAtIndex:0]
<MyEntity: 0x14c0628c0> (entity: MyEntity; id: 0xd00000070e740000 <x-coredata://8DAFE4F5-7218-4F6F-B32A-1BD9C056D1B9/MyEntity/p115613> ; data: {
"field1" = "joe";
"field2" = "doe";
})
控制檯輸出在視圖控制器
2015-11-19 17:51:09.188 MyApp[20299:7909725] Viewdidload: joe
2015-11-19 17:54:36.637 MyApp[20334:7913246] SettingsBarButtonClicked Log (null)
(lldb) po [self.masterArray count]
120441
(lldb) po [self.masterArray objectAtIndex:0]
<MyEntity: 0x1366db7a0> (entity: <null>; id: 0xd00000070e740000 <x-coredata://8DAFE4F5-7218-4F6F-B32A-1BD9C056D1B9/MyEntity/p115613> ; data: <fault>)
如果你看第二個'po'的輸出,你可以看到這個對象是一個'fault';這意味着核心數據將在第一次訪問時檢索實際對象。你有沒有試過訪問其中一個屬性? – Paulw11
爲什麼不在批量設置上設置批量大小?並顯示視圖控制器代碼。 – Wain
我更新了代碼以顯示視圖控制器和日誌。我注意到的一件事是,當單視圖在視圖中被調用時,會有大量的內存高峯,高達約145mb。之後,它下降到40mb左右。 ARC能否以某種方式清理單件或核心數據內容? – mikemike396