我有一个NSMutableDictionary,我添加到一些我异步收到的自定义NSObjects。每次我收到新数据我清除字典然后添加新对象。然后将数据推送到另一个类“UIView”以呈现字典中的内容。我一直在收到SIGABRT异常,据我所知,这意味着试图访问的对象已被释放。我已经尝试过同步块,创建一个mututablecopy并获取所有值但现在没有任何工作我的问题我如何实现同步
这是一段代码片段,
@interface MyAltViewController : UIViewController
{
__block NSMutableDictionary *currentDataList;
TestUIVIEW *myUIVIEW
}
@implementation MyAltViewController
......
- (void)viewDidLoad
{
currentDataList = [[NSMutableDictionary alloc] initWithCapacity:10];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(processDataMessag:)
name:view_Data object:nil];
}
.....
-(void)processDataMessag:(NSNotification *) notification
{
[currentDataList removeAllObjects];
NSArray tmpAr= (NSArray*) [notification object]
dispatch_async(dbQueue, ^(void)
{
/// Loop through the array and process the data then add to NSDictionary
[self pushtoMUViewLayer:currentDataList];
});
}
.........
-(void)pushtoMUViewLayer:(NSMutableDictionary *)ina
{
/// Even here if i try to access and object within
/// currentDataList and just a specific NSString i get the SIGABRT
[myUIVIEW updateWithData:ina];
}
//////////////////////////////////////////////////
@interface TestUIVIEW : UIView
{
NSMutableDictionary *mdata;
UIImage *outputImage ;
}
@property (assign) NSMutableDictionary *mapdata;
@property (retain) UIImage *outputImage ;
@implementation TestUIVIEW
.......
-(void)updateWithData:(NSMutableDictionary *)data
{
mdata = [data retain]; // ??? not sure how to set it correctly
dispatch_async(dispatch_get_main_queue(), ^(void)
{
[self CreateImage];
[self setNeedsDisplay];
});
}
-(void) CreateImage
{
NSString *key;
for(key in mapdata)
{
DataMessage *tmpData= (DataMessage*)[mapdata objectForKey:key];
NSString *aID = [ tmpData alt] ;
double aLat = [ tmpData lat] ;
double aLong = [ tmpData lon] ;
/*
Drawing code I can Get aLat & A Long
but it fails with SiABRT when trying to render
aID
/*
}
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGPoint imagePoint = CGPointMake(0, 0);
[outputImage drawAtPoint:imagePoint];
}
///如果同步不起作用。我可以使用NSNotification来查看我的currentDataList //到我的currentDataList
答案 0 :(得分:0)
在我访问NSDictionary的所有地方添加@synchronised块确实解决了问题。感谢大家的投入