, 나는 클래스의 새 인스턴스를 만들고 다음과 같이, 백그라운드에서 클래스 함수를 실행하는 데 NSOperationQueue를 사용하여 내 주요 파일이 RunEvents에 물건이 있지만 UI에 데이터를 전달하는 데 문제가 있습니다. 나는 스토리 보드에서 MyClass를 호출하는 메인 클래스에서 할 수있는 라벨을 업데이트하려하지만 MyClass에서 어떻게 업데이트합니까?NSOperationQueue 및 데이터 전달 내 응용 프로그램에서
2
A
답변
2
일반적인 대답은 클래스를 NSOperation
하위 클래스로 만들고 사용자 지정 완성 블록을 제공하는 것입니다.
// CustomOperation.h
#import <Foundation/Foundation.h>
typedef void(^CustomOperationCompletion)(NSString *string);
@interface CustomOperation : NSOperation
@property (nonatomic, copy) CustomOperationCompletion customOperationCompletion;
- (id)initWithCustomCompletion:(CustomOperationCompletion)completion;
@end
및
// CustomOperation.m
#import "CustomOperation.h"
@implementation CustomOperation
- (id)initWithCustomCompletion:(CustomOperationCompletion)completion {
self = [super init];
if (self) {
self.customOperationCompletion = completion;
}
return self;
}
- (void)main {
NSLog(@"%s starting", __FUNCTION__);
sleep(5);
NSString *string = [[NSDate date] description];
if (self.customOperationCompletion) {
[[NSOperationQueue mainQueue] addOperationWithBlock: ^{
self.customOperationCompletion(string);
}];
}
NSLog(@"%s ending", __FUNCTION__);
}
@end
그런 다음 당신이 뭔가를 호출 할 수 있습니다 : 당신의 목표는 UI 또는 완료 블록의 일부 모델 객체를 업데이트 할 경우, 메인 큐에 다시 그 블록을 파견해야합니다 같은 :
분명히NSOperationQueue *queue = [[NSOperationQueue alloc] init];
CustomOperation *operation = [[CustomOperation alloc] initWithCustomCompletion:^(NSString *string) {
// update the UI with the results of the operation; here I'm just going to log it
NSLog(@"all done, string=%@", string);
}];
[queue addOperation:operation];
, 당신은 (난 그냥 다시 문자열을 전달하고 있습니다)를 돌려 원하는 포함하도록 CustomOperationCompletion
매개 변수를 변경합니다.
+1
매우 유용한 답변입니다. 고마워요 @!! –
관련 문제
- 1. ntier 응용 프로그램에서 데이터 전달
- 2. WIFI 및 내 응용 프로그램에서 모바일 데이터
- 3. 다른 응용 프로그램에서 양식간에 데이터 전달
- 4. WinForms 응용 프로그램에서 MVC3으로 데이터 전달
- 5. 내 응용 프로그램에서 데이터 저장소
- 6. NSOperation 및 NSOperationQueue 고정 응용 프로그램
- 7. iphone - 서버로 데이터 보내기 및 NSOperationQueue 사용하기
- 8. asp.net 응용 프로그램에서 다른 호스트의 SL 응용 프로그램으로 데이터 전달
- 9. NSOperation 및 NSOperationQueue
- 10. 내 응용 프로그램에서 URL을
- 11. NSOperationQueue 및 ASIHTTPRequest
- 12. NSOperationQueue 및 블록 사용
- 13. 데이터 삭제 앱 내 응용 프로그램에서
- 14. 레일로 리디렉션 '데이터 :,'내 응용 프로그램에서
- 15. 내 C# 응용 프로그램에서 브라우저로 데이터 보내기
- 16. 인코딩하는 방법을 XML 데이터 내 응용 프로그램에서
- 17. 완료되면 NSOperationqueue 및 postnotification
- 18. 내 MVC3 응용 프로그램에서 리디렉션 때 나는 내 응용 프로그램에서
- 19. NSOperationQueue 및 UITableView
- 20. 내 엠버 응용 프로그램에서
- 21. Linq에 내 응용 프로그램에서
- 22. 내 응용 프로그램에서 안드로이드
- 23. NSOperationQueue 및 동시 작업
- 24. NSOperationQueue 및 NSOperation을 릴리스해야합니까?
- 25. Windows 응용 프로그램에서 인수 전달
- 26. 내 응용 프로그램에서 단축키
- 27. 내 응용 프로그램에서
- 28. 내 응용 프로그램에서 응용 프로그램에서 구현하는 방법
- 29. 내 Windows 응용 프로그램에서
- 30. 내 RoR에 응용 프로그램에서
이것은 NSOperationQueue http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues에 대한 온라인 자료입니다 – Rocker
당신의 작업에'completionBlock' 속성을 사용할 수 있습니다 ' –