2015-04-20 24 views
7

Tôi đã triển khai NSNotificationCenter trong ứng dụng của mình. Tôi gửi thông báo khi bao giờ giải mã hình ảnh được thực hiện. lần đầu tiên giải mã hình ảnh sẽ được thực hiện 8 lần. Do đó, thông báo giả sử gửi 8 lần.Nhưng nó đang gọi 64 lần (8 * 8).NSNotificationCenter đang gọi nhiều lần

Đây là mã của tôi làm thế nào tôi đã thực hiện -> // initialisation

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
if (self) { 

    [[NSNotificationCenter defaultCenter] addObserver:self 
                 selector:@selector(getHRImage:) 
                  name:@"DecodeComplete" object:nil];} 

// Gọi Phương pháp

-(void)getHRImage:(NSNotification *) notification 
{ 

if ([[notification name] isEqualToString:@"DecodeComplete"]) 
    NSLog (@"Successfully received the DecodeComplete notification! "); 
}` 

// Deallocation

- (void) dealloc 
{ 
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"DecodeComplete" object:self]; 
    //[super dealloc]; 
} 

// bài viết Thông báo

[[NSNotificationCenter defaultCenter] postNotificationName:@"DecodeComplete" object:self]; 

Một số người có thể gợi ý cho tôi nơi tôi đã làm sai.

Xin cảm ơn trước.

// Gọi phương pháp là như thế này (gọi 8 lần)

-(void)decode 
{ 
    NSLog(@"---------- Decoding is Complete ---------"); 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MdjDecodeComplete" object:self]; 

} 
+2

Bạn chưa hiển thị đủ ngữ cảnh xung quanh cuộc gọi của mình tới 'postNotificationName' để cung cấp câu trả lời, nhưng bạn phải gọi số này 64 lần - vì vậy tôi nghi ngờ có vấn đề với cấu trúc vòng lặp của bạn. Tôi đề nghị bạn nên đăng thêm mã hoặc đặt một điểm ngắt trên 'postNotificationWithName' và xem nơi nó đang được gọi là – Paulw11

+0

bạn đã kiểm tra xem phương thức' dealloc' của bạn có được gọi hay không? –

+0

có vẻ như bạn đang thêm quan sát nhiều lần và quan sát được thêm trước đó của bạn không bị loại bỏ để bạn nhận được phương thức getHRImage nhiều lần. –

Trả lời

5

Giải pháp: tôi kiểm tra lại mã của tôi, các initWithFrame: (CGRect) khung đang kêu gọi 8 lần và thêm NSNotification quan sát 8 lần.

Vì vậy, tôi đã thay đổi mã của mình như thế này, --- >> Initialisation.

static bool note=YES; 
- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
    if(note) 
[[NSNotificationCenter defaultCenter] addObserver:self 

                selector:@selector(getHRImage:) 
                 name:@"DecodeComplete" object:nil]; note=NO;} 

--- >> Deallocation

- (void) dealloc 
    { 
    note=true; 

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"DecodeComplete" object:nil]; 
//[super dealloc]; 
} 

Bây giờ addObserver phương pháp được gọi một lần duy nhất để vấn đề của tôi đã được giải quyết. Cảm ơn tất cả vì sự hướng dẫn quý giá của bạn.

-2

- (void) dealloc sẽ không được gọi trong môi trường ARC. Instread, bạn có thể loại bỏ quan sát của bạn trước khi thêm nó như thế này:

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"DecodeComplete" object:self]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getHRImage:) name:@"DecodeComplete" object:nil];  
    } 
} 
+5

dealloc sẽ được gọi trong ARC, vì vậy mọi tùy chỉnh trong dealloc vẫn hợp pháp. Bạn phải phạm sai lầm ở đây. – user3349433

+1

@Lazy_Cluctch nó không hoạt động. –

+0

Điều này không làm việc vì hai lý do: 1. '-dealloc' được thực hiện như được đề cập bởi @ user3349433, 2. nó sẽ loại bỏ trường hợp * mới * xem, không phải là một hiện có. –

Các vấn đề liên quan