iOS - 다중 스레드 종속(업데이트 예정)

6159 단어
1. 서브스레드에서 대리 리셋
    **          Runloop,  delegate           **
    **          runloop  runloop        nsmachport       ,          ,      ,              **

    - (void)location{
        dispatch_queue_t deleQue = dispatch_queue_create("haha", DISPATCH_QUEUE_SERIAL);
        dispatch_async(deleQue, ^{
            _runloop = [NSRunLoop currentRunLoop];
            _port = [NSMachPort port];
            [_runloop addPort:_port forMode:NSDefaultRunLoopMode];
            self.locationManager = [[CLLocationManager alloc] init];
            self.locationManager.delegate = self;
            self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
            [self startLocation];
            [_runloop run];
        });
    }
    
    - (void)startLocation
    {
        if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [self.locationManager requestWhenInUseAuthorization];
        }
        [self.locationManager startUpdatingLocation];
        NSLog(@"    ");
    }
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        CLLocation *location = [locations lastObject];
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        //     
        [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
            if (error || [placemarks count] == 0)
            {
                NSLog(@"       ,error = %@", error);
                return;
            }
            NSDictionary *addressDic = [[placemarks objectAtIndex:0] addressDictionary];
            [_runloop removePort:_port forMode:NSDefaultRunLoopMode];
            if (_locationSuccess) {
                _locationSuccess(addressDic);
            }
            NSLog(@"dic = %@",addressDic);
        }];
        [manager stopUpdatingLocation];
    }
    
    - (void)locationManager:(CLLocationManager *)manager
           didFailWithError:(NSError *)error
    {
        if (error.code == kCLErrorDenied) {
        }
        NSLog(@"    ");
    }

2. 스레드 의존성
1)//NSOperationQueue 스레드 이전에 종속 작업 추가
-(void)dependency{
    /**
        A、B、C    ,  :
     1. 3        
     2.   C     B
     3.   B     A
     */
    //      
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    //          
    queue.maxConcurrentOperationCount = 3;

    //      
    NSBlockOperation *operationA = [NSBlockOperation blockOperationWithBlock:^{
       NSLog(@"A       :%@", [NSThread currentThread]);
    }];

    NSBlockOperation *operationB = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"B       :%@", [NSThread currentThread]);
    }];

    NSBlockOperation *operationC = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"C       :%@", [NSThread currentThread]);
    }];

    //          
    // operationB       operationA
    [operationB addDependency:operationA];
    // operationC       operationB
    [operationC addDependency:operationB];


    //        (        ,  )
    [queue addOperation:operationA];
    [queue addOperation:operationB];
    [queue addOperation:operationC];
    [NSOpeationQueue mainQueue] addOperation ^{
    }];

}

** 2)GCD**
     dispatch_group_async block          ,                              dispatch_group_notify     ,    dispatch_group_enter dispatch_group_leave         :
    #pragma mark -         
    - (void)downloadBaseData  
    {  
        //     group  
        group = dispatch_group_create();  
        //       
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);  
          
        //    (             ,        )  
        dispatch_group_enter(group);  
        dispatch_group_async(group, queue, ^{  
            //       1  
            [self fetchBaseData];  
        });  
          
        //      
        dispatch_group_enter(group);  
        dispatch_group_async(group, queue, ^{  
            //       2  
            [self fetchInspectorBaseData];  
        });  
          
        dispatch_group_notify(group, dispatch_get_main_queue(), ^{  
            [SVProgressHUD dismiss];  
            ILog(@"          !");  
            [[AppDelegate sharedDelegate] showMainView];  
        });

3. 목표 대기열 관련
        +(void)testTargetQueue {    
            dispatch_queue_t targetQueue = dispatch_queue_create("test.target.queue", DISPATCH_QUEUE_SERIAL);    
                
                
                
            dispatch_queue_t queue1 = dispatch_queue_create("test.1", DISPATCH_QUEUE_SERIAL);    
            dispatch_queue_t queue2 = dispatch_queue_create("test.2", DISPATCH_QUEUE_SERIAL);    
            dispatch_queue_t queue3 = dispatch_queue_create("test.3", DISPATCH_QUEUE_SERIAL);    
                
            dispatch_set_target_queue(queue1, targetQueue);    
            dispatch_set_target_queue(queue2, targetQueue);    
            dispatch_set_target_queue(queue3, targetQueue);    
                
                
            dispatch_async(queue1, ^{    
                NSLog(@"1 in");    
                [NSThread sleepForTimeInterval:3.f];    
                NSLog(@"1 out");    
            });    
            
            dispatch_async(queue2, ^{    
                NSLog(@"2 in");    
                [NSThread sleepForTimeInterval:2.f];    
                NSLog(@"2 out");    
            });    
            dispatch_async(queue3, ^{    
                NSLog(@"3 in");    
                [NSThread sleepForTimeInterval:1.f];    
                NSLog(@"3 out");    
            });    
                
                
                
        }    
            
              
         1 in    
         1 out    
         2 in    
         2 out    
         3 in    
         3 out 

4. 라인 안전?
라인 자물쇠

좋은 웹페이지 즐겨찾기