iOS:对于一个简单的 iOS 应用程序来说,这是一个合理的设计模式吗?
iOS: Is this a reasonable design pattern for a simple iOS App?
我正在构建一个向 API 发出请求的简单应用程序。没什么太复杂的。我只是想知道这是否是应用程序的良好设计模式。我正在做一些与我通常做的不同的事情,因为这种设计模式看起来更简洁。我知道对于什么是更好的设计模式有意见,但在可接受和不可接受之间有明确的界限。这就是我想要找出的。
编辑:为了简化问题,对于向服务器发出 API 请求并在控制器之间传递对象的应用程序来说,这是一个好的设计模式吗?
视图控制器1.m .
- (IBAction)login:(id)sender {
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setValue:self.usernameField.text forKey:@"username"];
[params setValue:self.passwordField.text forKey:@"password"];
[apiClient loginRequest:params onSuccess:^(User *userInfo) {
ViewController2 *viewController2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
viewController2.userInfo = userInfo;
[self.navigationController pushViewController:viewControllers animated:YES];
}onFailure:^(NSError* error) {
}];
}
APIClient.m
- (void)loginRequest:(NSMutableDictionary *)params
onSuccess:(void(^)(id response))successBlock
onFailure:(void (^)(NSError *))failureBlock
{
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[manager POST:[NSString stringWithFormat:@"%@/login", URL] parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
User *user = [[User alloc]init];
user.name = [responseObject objectForKey:@"name"];
successBlock(user);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failureBlock(error);
}];
}
User.h(这是模型对象)
#import <Foundation/Foundation.h>
@interface User : NSObject
@property (nonatomic,copy) NSString *name;
// There is more properties, but I excluded them from this example
@end
您不应将实现放入头文件中(如 APIClient.h)。
您的代码看起来不错,但请详细说明您应用的结构。
在我看来,这个模式有两个方面可以做得更好。
1) 当前用户
由于您的示例是针对当前用户的,因此最好将其放在单例中以便像这样使用(来自 parse.com iOS SDK):
PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
// do stuff with the user
} else {
// show the signup or login screen
}
实现起来非常简单,并且是保存用户的更好位置,而不是将其从 vc 传递到 vc。
2) 在将要使用模型的 vc 之前对模型进行 api 调用在我看来不是最好的方法。
当 api 调用正在获取模型时,我会推送 vc 和具有 activity 指示器的 onViewLoad。
我正在构建一个向 API 发出请求的简单应用程序。没什么太复杂的。我只是想知道这是否是应用程序的良好设计模式。我正在做一些与我通常做的不同的事情,因为这种设计模式看起来更简洁。我知道对于什么是更好的设计模式有意见,但在可接受和不可接受之间有明确的界限。这就是我想要找出的。
编辑:为了简化问题,对于向服务器发出 API 请求并在控制器之间传递对象的应用程序来说,这是一个好的设计模式吗?
视图控制器1.m .
- (IBAction)login:(id)sender {
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setValue:self.usernameField.text forKey:@"username"];
[params setValue:self.passwordField.text forKey:@"password"];
[apiClient loginRequest:params onSuccess:^(User *userInfo) {
ViewController2 *viewController2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
viewController2.userInfo = userInfo;
[self.navigationController pushViewController:viewControllers animated:YES];
}onFailure:^(NSError* error) {
}];
}
APIClient.m
- (void)loginRequest:(NSMutableDictionary *)params
onSuccess:(void(^)(id response))successBlock
onFailure:(void (^)(NSError *))failureBlock
{
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[manager POST:[NSString stringWithFormat:@"%@/login", URL] parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
User *user = [[User alloc]init];
user.name = [responseObject objectForKey:@"name"];
successBlock(user);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failureBlock(error);
}];
}
User.h(这是模型对象)
#import <Foundation/Foundation.h>
@interface User : NSObject
@property (nonatomic,copy) NSString *name;
// There is more properties, but I excluded them from this example
@end
您不应将实现放入头文件中(如 APIClient.h)。
您的代码看起来不错,但请详细说明您应用的结构。
在我看来,这个模式有两个方面可以做得更好。
1) 当前用户 由于您的示例是针对当前用户的,因此最好将其放在单例中以便像这样使用(来自 parse.com iOS SDK):
PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
// do stuff with the user
} else {
// show the signup or login screen
}
实现起来非常简单,并且是保存用户的更好位置,而不是将其从 vc 传递到 vc。
2) 在将要使用模型的 vc 之前对模型进行 api 调用在我看来不是最好的方法。 当 api 调用正在获取模型时,我会推送 vc 和具有 activity 指示器的 onViewLoad。