0

I am using AfNetworking to make POST requests. After I received response in completion block, progress hud view dismissed and in NSUserDefaults some data is saved. But when I run application, UI does not update and waits till the app crashes for a memory problem.

This is my sample code:

[[HTAPIClient sharedClient] POST:requestAddress
                      parameters:nil
                         success:^(NSURLSessionDataTask *task, id responseObject) {
                              if ([responseObject objectForKey:@"default_source"]) {
                                  NSUserDefaults *usd = [NSUserDefaults standardUserDefaults];
                                  [usd setObject:[responseObject objectForKey:@"default_source"] forKey:@"payViaCreditCard"];
                                  [usd synchronize];
                              }

                              NSDictionary *sources = [responseObject objectForKey:@"sources"];
                              if (sources) {
                                  NSArray *data = [sources objectForKey:@"data"];
                                  if (data) {
                                      // some code
                                  }
                              }

                              [self.navigationController popViewControllerAnimated:YES];
                              [SVProgressHUD dismiss];
                         }
                         failure:^(NSURLSessionDataTask *task, NSError *error) {
                             // some failure
                         }];

Any idea how to solve this problem?

Thanks for any advance!

EDIT:

This is what say XCode in the end:

The app "MyApp" on "iPhone" quit unexpectedly. Message from debugger: Terminated due to Memory Error

Shyngys Kassymov
  • 718
  • 11
  • 21
  • I haven't checked in AFN2, but I guess the success block is called on the main thread (unless you've changed it) ? – Wain Apr 07 '15 at 12:22
  • Yes, success block is called on the main thread and I did not change the behaviour. – Shyngys Kassymov Apr 07 '15 at 12:25
  • Could you please check if it happens when you cast your response object as NSDictionary before using it? – LoVo Apr 07 '15 at 12:34
  • When I check with breaks, I can see that all code in block runs successfully. But nothing happens with UI. – Shyngys Kassymov Apr 07 '15 at 12:39
  • You should identify the source of the memory error. It is not in the code that you've shared with us thus far. Could be some wasteful use of memory (e.g. loading many large assets (e.g. images, movies, etc.) into memory at the same time), use of huge loop and autorelease objects, or some infinite recursion. Through use of debugger, breakpoints or log statements, identify where it's getting tied up. – Rob Apr 07 '15 at 12:41
  • This post might be the solution for your problem: http://stackoverflow.com/questions/17225193/are-afnetworking-success-failure-blocks-invoked-on-the-main-thread – LoVo Apr 07 '15 at 12:43
  • @Chika Are you saying that (a) it's hitting the `popViewControllerAnimated` line; (b) `self.navigationController` is definitely not `nil`; but that (c) the view is not getting dismissed? – Rob Apr 07 '15 at 12:44

1 Answers1

1

Try this

 NSDictionary *sources = [responseObject objectForKey:@"sources"];
                          if (sources) {
                              NSArray *data = [sourcesobjectForKey:@"data"];
                              if (data) {
                                  // some code
                              }
                          }

 dispatch_async(dispatch_get_main_queue(), ^{
           [SVProgressHUD dismiss];
         [self.navigationController popViewControllerAnimated:YES];

        });
iAnurag
  • 9,286
  • 3
  • 31
  • 48