I want to reload the table at the end of asynchronous communication.

Asked 1 years ago, Updated 1 years ago, 76 views

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.

ios objective-c mvvm

2022-09-30 21:12

1 Answers

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)

  • Make your own delete and call the delete method after processing is complete
  • Put Block syntax in method arguments

Each characteristic is

[Delegate Features]

  • Less nested sources
  • Optional eliminates the need to write the receiving process on the transfer side and prevents the source from becoming rough when using it in multiple locations
  • Cannot keep track of the source in series

BlCharacteristics of Block Syntax <

  • Easy to follow source processing in series (&write processing in series)
  • Reduce the number of methods in the model (delegate adds as many methods as needed)
  • Reduce the number of objects defined on the calling side of the model
  • Deep source nesting

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,

  • Not consecutive asynchronous processing→delegate
  • Continuous asynchronous processing → Block syntax

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.


2022-09-30 21:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.