gpt4 book ai didi

objective-c - 具有异步操作和 UI 的顺序执行流程

转载 作者:太空狗 更新时间:2023-10-30 03:41:58 25 4
gpt4 key购买 nike

通常,在 GUI 中,我遇到的程序流程本质上是顺序的,但涉及异步操作(例如 NSURLConnection 下载东西)和 UI 操作(等待用户选择一个UIActionSheet 中的选项)。

举例说明,

  1. 显示 UIActionSheet,提示用户选择颜色。
  2. 如果颜色是蓝色,从服务器下载一个文件。
    1. 如果下载失败,通知用户(UIAlertView,等待点击OK)
    2. 如果用户在 UIAlertView 中选择 YES,请重试下载。
  3. 如果颜色为黑色,则执行其他操作。
  4. 等等等等。

流程是顺序的 - 我们不会在 1 之前执行 2。但是由于异步操作和 UI 元素,如果我们使用委托(delegate)(或 block ),这很快就会变成意大利面条代码。

是否有编写此类代码的通用方法?

最佳答案

有一个图书馆叫Reactive Cocoa这太棒了,但很难习惯。

实现目标的一种更简单但不是那么棒的方法是使用 blocks wrapper围绕 UIAlertView 和 UIActionSheet。此外,这还假设您的网络代码中有回调 block 。

例子:

- (void)showActionSheet
{
BlockActionSheet *sheet = [BlockActionSheet sheetWithTitle:@"Choose one"];
__weak BlockActionSheet *weakSheet = sheet;

[sheet addButtonWithTitle:@"Blue" atIndex:0 block:^{
[self downloadFileFromServerSuccessBlock:^{
//YAY
} failureBlock:^{
BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Failure" message:@"Something Went Wrong"];
[alert addButtonWithTitle:@"Try Again" block:^{
[weakSheet showInView:self.view];
}];
[alert setCancelButtonWithTitle:@"Cancel" block:nil];
}];
}];

[sheet addButtonWithTitle:@"Black" atIndex:1 block:^{
//something else
}];

[sheet setCancelButtonWithTitle:@"Cancel" block:nil];

[sheet showInView:self.view];
}

所以最后一行“[sheet showInView:self.view]”开始了整个过程。如果他们选择蓝色,那么该 block 将被调用。您的网络代码还支持 block ,因此您可以从那里获得成功和失败的 block 回调。如果失败,那么您会弹出一个基于 block 的警报 View ,使 ActionSheet 再次显示自己。

我希望这至少对您有所帮助。此外,“self.view”调用可能会发生一些强引用,因此我也会对 View 进行弱引用。

这是 ReactiveCocoa 的例子。我对这个框架很陌生,所以我希望我能正确使用它。

/*
* This function is getting called from a login view controller.
*/

- (void)sendAuthentication
{
/*
* A RACSubscribable will invoke the where^ block when it calls [sendNext:];
* That call is coming from the ThaweQBRequestController.
* If the where^ block return YES then the subscribeNext^ block will get called.
* asMaybes wraps the object I send back in a RACMaybe object which has a convenience method [hasObject];
* The subscribeNext^ block has an object coming into it, that object is an array in this case that I sent
* from the ThaweQBRequestController.
*/

RACSubscribable *sub = [[ThaweQBRequestController logInWithUsername:self.thaweusername password:self.thawepassword] asMaybes];

[[sub where:^(id x) {
return [x hasObject];
}]
subscribeNext:^(id _) {

NSArray *array = [_ object];

NSString *errcode = [array objectAtIndex:0];

if (errcode.boolValue == YES)
//YAY SUCCESS!!!

} else {
//UH OH
}
// Update UI, the array has the username and password in it if I want to display or whatever is clever.
}];
}

然后我们进入我的网络请求 Controller 。

+ (RACAsyncSubject *)logInWithUsername:(NSString *)username password:(NSString *)password
{
/*
* First we have our loginCommand. It will invoke the block we give it on a background thread.
* This block returns a Subscribable. We can subscribe other blocks to be invoked when the aync call is done.
* In this case we are creating loginResult to retrieve the async call.
*/

RACAsyncCommand *loginCommand = [RACAsyncCommand command];

/*
* Now we have our AsyncSubject. We are instantiated it here and are going to pass it back to the login view controller;
* When our async call finishes and we filter through the results we will call [subject sendNext:] to invoke blocks we created in
* the login view controller;
* We will filter the results uses the loginResult blocks.
*/
RACAsyncSubject *subject = [RACAsyncSubject subject];

/*
* loginResult is a "Subscribable". Every time it gets a [sendNext:] call it runs the blocks assosiated with it.
* These [sendNext:] calls are coming from our network code.
* 'repeat' means that even after the async block is invoked it keeps a reference to it incase we want to use it again.
* 'asMaybes' wraps a RACMaybe object around the object you send to the loginResult blocks. The benefit of a RACMaybe is
* that it has some convienence methods like 'hasError' and 'hasObject'.
*/

RACSubscribable *loginResult = [[[loginCommand
addAsyncBlock:^(id _) {
return [self authenticateUser:username password:password];
}]
repeat]
asMaybes];

/*
* The first block, where^, get called every time loginResult calls [sendNext:].
* If it returns YES then the select^ block gets called. The select^ block invokes the subscribeNext^ block and sends it the
* error.
*/

[[[loginResult
where:^(id x) {
return [x hasError];
}]
select:^(id x) {
return [x error];
}]
subscribeNext:^(id x) {
NSLog(@"network error omg: %@", x);
}];

/*
* Same as above except this time we are looking for instances when we have an object instead of an NSError.
* The object we are getting is being returned from our network code. In this case it is an integer, 0 means we had a successfull login.
* Now we can call [subject sencNext:] to inform our login view controller of the pass or fail of the authentication.
* [sendCompleted] is a cleanup call, allowing ReactiveCocoa to dispose of our blocks and free up memory.
*/

[loginResult
where:^(id x) {
return [x hasObject];
}]
subscribeNext:^(id _) {
NSNumber *number;
NSString *errcode = [_ object];
if (errcode.intValue == 0) number = [NSNumber numberWithBool:YES] ?: [NSNumber numberWithBool:NO];
[subject sendNext:[NSArray arrayWithObjects:number, username, password, nil]];
[subject sendCompleted];
}];

/*
* [execute:] starts the whole thing off. This call invokes the loginCommand block that returns the async call to the loginResult subscribable.
*/

[loginCommand execute:@"This value gets transfered to the addAsyncBlock:^(id _) block above."];

return subject;
}

/*
* This function uses a QuickBase wrapper I made to make server request and whatnot.
* This function is called when the [loginCommand execute:] call in the previous function gets called
* and invokes the loginCommand block which returns the async request.
* That request is what this function is returning.
*/

+ (RACAsyncSubject *)authenticateUser:(NSString *)username password:(NSString *)password
{
QBRequest *request = [[QBRequest alloc] init];

[request setQuickBaseAction:QBActionTypeAuthenticate];

[request setURLString:URLstring withDatabaseID:nil];

[request setApplicationToken:appToken];

return [request sendAndPersist:NO username:username password:password];
}

现在我们在我的实际网络包装器中,它知道请求何时完成或失败或其他什么。

- (RACAsyncSubject *)sendAndPersist:(BOOL)persist username:(NSString *)username password:(NSString *)password
{
self.subject = [RACAsyncSubject subject];

/*
* These next two blocks are called when my network request is done.
* My AsyncSubject is a property so that I can reference it later when I parse
* throught the response and figure out whether I logged in correctly or not.
* In the case that the network call itself fails, then the AysncSubject calls
* [sendError:] which will invoke those NSError capturing blocks in the ThaweQBRequestController.
*/

[anOp onCompletion:^(MKNetworkOperation *completedOperation) {

dispatch_async(background_parseSave_queue(), ^{

[self updateDatabase];
});

} onError:^(NSError *error) {
[subject sendError:error];
}];

[engine enqueueOperation:anOp];

return subject;
}

最后让您了解我何时将主题 [sendNext:] 放入我的解析器中。self.currentParsedCharacterData 是一个具有整数值的 NSString。

else if ([elementName isEqualToString:@"errcode"])
{
if ([self.action isEqualToString:@"API_Authenticate"]) {
[subject sendNext:[self.currentParsedCharacterData copy]];
[subject sendCompleted];
}
}

我知道这很长,但我真的很想给出一些实际的代码示例。

关于objective-c - 具有异步操作和 UI 的顺序执行流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12063636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com