NSOperation and NSNotificationCenter on the main thread

后端 未结 4 1628
挽巷
挽巷 2020-12-12 23:02

I have an NSOperation. When it is finished I fire a NSNotificationCenter to let the program know that the NSoperation is finished and to update the gui.

To my under

4条回答
  •  Happy的楠姐
    2020-12-12 23:28

    To expand on Danra's answer here's the ARC compliant version of the category I put together:

    NSNotificationCenter+Threads.h

    @interface NSNotificationCenter (Threads)
    
    -(void)postNotificationOnMainThread:(NSNotification *)notification;
    -(void)postNotificationNameOnMainThread:(NSString *)name object:(id)object;
    -(void)postNotificationNameOnMainThread:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo;
    
    @end
    

    NSNotificationCenter+Threads.m

    @implementation NSNotificationCenter (Threads)
    
    -(void)postNotificationOnMainThread:(NSNotification *)notification
    {
        [self performSelectorOnMainThread:@selector(postNotification:) withObject:notification waitUntilDone:NO];
    }
    
    -(void)postNotificationNameOnMainThread:(NSString *)name object:(id)object
    {
        [self postNotificationNameOnMainThread:name object:object userInfo:nil];
    }
    
    -(void)postNotificationNameOnMainThread:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self postNotificationName:name object:object userInfo:userInfo];
        });
    }
    
    @end
    

提交回复
热议问题