I have a question about how to implement it in MVVM.
Model uses AFNetWorking to obtain JSON.
I would like to reload the View table when AFNetWorking's asynchronous communication ends, but in the case of MVVM, I understand that the Model does not operate the View.
Please tell me how to update the View in this case.
//Model
[manager POST:testAPI
parameters —parameters
success:^(NSURLSessionDataTask*task, id responseObject)
{
// Register Response in DB
// TODO: I want to update the View after DB registration
} failure:^(NSURLSessionDataTask*task, NSError*error)
{
}];
[delegate]
Even model.delegate=self in View will be nil when AFNetWorking blocks...
I just made a mistake in creating an instance of the Model.
ockblocks <
This is the flow of View→ViewModel→Model...
//View
[_viewModel sampleMethod1:^{
self refresh;
}];
// ViewModel
- (void)sampleMethod1:(void(^)(void))refreshView{
[_model sampleMethod2:^{
refreshView();
}];
// Model
- (void)sampleMethod2:(void(^)(void))refreshView{
..........
[manager POST:testAPI
parameters —parameters
success:^(NSURLSessionDataTask*task, id responseObject)
{
// Performs the process of registering a response to DB
.......
// TODO: After the above DB registration process, I want to update the View process.
refreshView();
} failure:^(NSURLSessionDataTask*task, NSError*error)
{
}];
}
Therefore, the processing from V to VM was further passed to M, and I felt that it was redundant.
I think there is a better way to write, but I don't know...
I don't know how to write in the case of Blocks, but I think I could implement it with delete.
There are about two ways to inform you that the processing of the methods in the model has been completed.
(Write if asynchronous processing is included)
Each characteristic is
[Delegate Features]
BlCharacteristics of Block Syntax <
and so on.
At first, I think it's okay to use either of them.
In my case, I use both, but
To use it differently,
In many cases, it may be
How do I write
[How to write delete]
Model.h as below
//Define Deligate
@protocol WebClientDelegate<NSObject>
- (void) didSuccess: (NSDictionary* response);
- (void) didFailure: (NSError* error);
@end
@ interface Model:NSObject
/// Properties for Reference at Deligated Destination
@property(assign,nonatomic)id<ModelDelegate>delegate;
....
@end
Model.m using the following methods:
- (void) sampleMethod
{
[manager POST:testAPI
parameters —parameters
success:^(NSURLSessionDataTask*task, id responseObject)
{
// Register Response in DB
// TODO: I want to update the View after DB registration
NSDictionary* result = "Some result"
if([self.delegate responsesToSelector:@selector(didSuccess:)]){
[self.delegate didSuccess:result];
}
} failure:^(NSURLSessionDataTask*task, NSError*error){
if([self.delegate responsesToSelector:@selector(didFailure:)]){
[self.delegate didFailure:error];
}
}];
}
】How to write Block syntax /
-(void)sampleMethod:(void(^)(NSDictionary*response)) success
failure: (void(^)(NSError*error)) failure
{
[manager POST:testAPI
parameters —parameters
success:^(NSURLSessionDataTask*task, id responseObject)
{
// Register Response in DB
// TODO: I want to update the View after DB registration
NSDictionary* result = "Some result"
success(result);
} failure:^(NSURLSessionDataTask*task, NSError*error){
failure(error);
}];
}
will be
I hope this will be helpful.
© 2024 OneMinuteCode. All rights reserved.