iOS - 다중 스레드 종속(업데이트 예정)
** 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. 라인 안전?
라인 자물쇠
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.