Objective-C在Singleton中调用实例方法
我创建了一个名为Objective-C在Singleton中调用实例方法,objective-c,ios,Objective C,Ios,我创建了一个名为DataManager的单例类。我已经设置了一些缓存。但是,当尝试从DataManager的另一个实例方法中调用缓存实例方法时,我在行中遇到EXC\u BAD\u ACCESS错误: NSMutableArray *stops = [[DataManager sharedInstance] getCacheForKey:@"stops"]; DataManager.h @interface DataManager : NSObject { FMDatabase *db;
DataManager
的单例类。我已经设置了一些缓存。但是,当尝试从DataManager
的另一个实例方法中调用缓存实例方法时,我在行中遇到EXC\u BAD\u ACCESS
错误:
NSMutableArray *stops = [[DataManager sharedInstance] getCacheForKey:@"stops"];
DataManager.h
@interface DataManager : NSObject {
FMDatabase *db;
NSMutableDictionary *cache;
}
+ (DataManager *)sharedInstance;
// ... more methods
- (id)getCacheForKey:(NSString *)key;
- (void)setCacheForKey:(NSString *)key withValue:(id)value;
- (void)clearCache;
DataManager.m
- (NSArray *)getStops:(NSInteger)archived {
NSMutableArray *stops = [[DataManager sharedInstance] getCacheForKey:@"stops"];
if (stops != nil) {
NSLog(@"Stops: %@", stops);
return stops;
}
stops = [NSMutableArray array];
// set stops...
[[DataManager sharedInstance] setCacheForKey:@"stops" withValue:stops];
return stops;
}
从另一个视图控制器调用时,似乎会发生这种情况。即第一视图控制器无错误,第二视图控制器有错误
这是我第一次尝试单身,所以我肯定我犯了一个简单的错误。但我自己却看不到
注意:我尝试了[self-getCache…]
,结果相同
更新
这是我的单例实现。改编自
您的
缓存
对象已自动删除,因此当您尝试访问它时,它不再位于内存中
使用
[NSMutableDictionary alloc]init]
而不是[NSMutableDictionary]
来获取保留的实例。您的缓存
对象被自动删除,因此当您尝试访问它时,它不再在内存中
使用
[NSMutableDictionary alloc]init]
而不是[NSMutableDictionary]
来获取保留的实例。不是对已回答的问题的直接回答
然而,我只想指出,您的单例实现是次优的@synchronized
非常昂贵,您可以避免每次访问singleton时使用它:
if (!instance) {
@synchronized(self) {
if (!instance) {
instance = [[super allocWithZone:NULL] init];
}
}
}
初始化单例的更好方法是:
+ (DataManager *)sharedInstance {
static DataManager *instance;
static dispatch_once_t donce;
dispatch_once(&donce, ^{
instance = [[self alloc] init];
});
return instance;
}
不是对你的问题的直接回答,你的问题已经得到了回答 然而,我只想指出,您的单例实现是次优的
@synchronized
非常昂贵,您可以避免每次访问singleton时使用它:
if (!instance) {
@synchronized(self) {
if (!instance) {
instance = [[super allocWithZone:NULL] init];
}
}
}
初始化单例的更好方法是:
+ (DataManager *)sharedInstance {
static DataManager *instance;
static dispatch_once_t donce;
dispatch_once(&donce, ^{
instance = [[self alloc] init];
});
return instance;
}
您在哪里初始化单例?在类方法
sharedInstance
中。显示sharedInstance
和setCacheForKey:withValue:
的实现。您正在显示的代码看起来不错,尽管您应该在实例方法中使用self
而不是sharedInstance
方法。我在第一次尝试创建单例时遇到了一个非常类似的错误。它没有正确执行。你能发布你的init和sharedInstance方法吗?getCacheForKey:
应该是cacheForKey:
。Cocoa/iOS不使用getter的get
前缀。在类方法sharedInstance
中,在哪里初始化singleton?显示sharedInstance
和setCacheForKey:withValue:
的实现。您正在显示的代码看起来不错,尽管您应该在实例方法中使用self
而不是sharedInstance
方法。我在第一次尝试创建单例时遇到了一个非常类似的错误。它没有正确执行。你能发布你的init和sharedInstance方法吗?getCacheForKey:
应该是cacheForKey:
。Cocoa/iOS不使用getter的get
前缀,也不将缓存设为保留属性doh。有道理。不过,这引出了另一个问题。理论上从未调用过dealloc
,我如何正确地释放它呢?嗯,你没有。它是一个单例。或者将缓存设为保留属性doh。有道理。不过,又引出了一个问题。理论上从未调用过dealloc
,我如何正确地释放它呢?嗯,你没有。这是一个单身汉,我以前读过。但是,当我支持iOS 4+时,我不确定这是否可以?不确定是否支持4.0+,但我可以保证4.2+:)谢谢对于支持4.2+的未来应用程序,我一定会记住这一点。我以前读过。但是,当我支持iOS 4+时,我不确定这是否可以?不确定是否支持4.0+,但我可以保证4.2+:)谢谢对于支持4.2+的未来应用程序,我一定会记住这一点。