1

i want update the text of my label every time i receive notification from nsmanageObjContext.

this is my code for add the observer:

- (IBAction)requestFotPhoto {

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(updateLabel) name:NSManagedObjectContextDidSaveNotification
                                           object:self.facebook.managedObjectContext];

and this is the method for update the label:

-(void)updateLabel
{
        NSString *text = [NSString stringWithFormat:@"Downalad %i pictures",[Photo NumeberOfAllPhotosFromContext:self.facebook.managedObjectContext]];
        dispatch_async(dispatch_get_main_queue(), ^{
            //UIKIT method
            NSLog(@"text %@",text);
            [self.downlaodLabel setText:text];
        });
}

i assume that updateLabel is execute in a another thread, so i execute the instructions for update the label on the main thread, but this code has no effect. where is the problem?

obviously the NSlog print the right message!

thanks!

Max_Power89
  • 1,710
  • 1
  • 21
  • 38
  • Do you see any warnings? I think you can't use variable you declared outside a block like that. – Enrico Susatyo Oct 06 '13 at 12:36
  • no warning or error. in other situation i use the local variable inside a block, and all works fine – Max_Power89 Oct 06 '13 at 12:45
  • Unless you're doing something strange, `IBAction` methods are called on the main thread. Is `downlaodLabel` actually set to something? – Wain Oct 06 '13 at 12:45
  • 'downloadLabel' is a property connect with the storyboard, if i try to set the label on 'requestFotPhoto' method, the label is set correctly. – Max_Power89 Oct 06 '13 at 12:52
  • Are you sure it's hooked up correctly? You are spelling `download` wrong in `self.downlaodLabel`... If the NSLog statement is working, then it's a connection issue – iwasrobbed Oct 06 '13 at 16:10
  • If the text is properly set when called in `IBAction` method, then it might not be a connection issue. Still, you can check if the label is not `null` before setting its text just for testing. Or maybe the text is changed to something else/original text right after that method set for the text on your label. Maybe you should lookout for that. – Zen Oct 06 '13 at 18:43

2 Answers2

0

In your situation you don't need to use dispatch_async, because notification handlers are run in the main thread. They are executed in a main loop on idle moments — sorry if I'm wrong with techincal words, english is not native for me.

And one more thing: you should't reference self from blocks, because self points to your block, and block points to self — they're not going to be released. If you really want to do it, you can read this question.

Community
  • 1
  • 1
Stanislav
  • 410
  • 3
  • 11
  • if i leave dispatch_async is the same! it doesn't work. maybe the problem is that method is call a lot of time, so the label dont have time to refresh itself – Max_Power89 Oct 06 '13 at 18:32
  • Set a breakpoint — maybe `self.downlaodLabel==nil`? It can be if your function is called before the view is layed out. You can also try `NSLog(@"%@", self.downlaodLabel.text)` right after you set a value. If you see this value — it means, that it gets changed somewhere else after this. – Stanislav Oct 06 '13 at 20:30
  • You can also try to wrap you method in `[self performSelector:@selector(innerUpdateLabel) withObject:nil afterDelay:0]` and see what happens – Stanislav Oct 06 '13 at 20:41
  • the downloadLabel is not nil. i have solved by putting the label in `requestFotPhoto` and now works correctly. but i don't know which is the original problem! – Max_Power89 Oct 06 '13 at 21:46
0

seems like:

  • your should move your NSNotificationCenter addObserver code, from your (IBAction)requestFotPhoto (seems is some button click event handler, which only run after user tapped) to viewDidLoad

shold like this:

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLabel) name:NSManagedObjectContextDidSaveNotification object:self.facebook.managedObjectContext];
}
  • and for noficacation handler, not use dispatch_async

should like this:

- (void)updateLabel:(NSNotification *) notification {
    NSLog (@"updateLabel:  notification=%@", notification);
    if ([[notification name] isEqualToString: NSManagedObjectContextDidSaveNotification]) {
        NSDictionary *passedInUserInfo = notification.userInfo;
        NSString *yourText = [passedInUserInfo objectForKey:@"dataKey"];

        //UIKIT method
        NSLog(@"yourText=%@",yourText);
        [self.downlaodLabel setText:yourText];
    }
}
  • and somewhere else should send the text:
NSString *newText = @"someNewText";
NSDictionary *passedInfo = @{@"dataKey": newText};
[[NSNotificationCenter defaultCenter] postNotificationName:NSManagedObjectContextDidSaveNotification object: self userInfo:passedInfo];

for more detail pls refer another post answer

crifan
  • 12,947
  • 1
  • 71
  • 56