Problem receiving the return of an object

Asked

Viewed 60 times

1

I created a class to download data from a web service. And I take the following test steps:

I instate the download class in viewDidLoad of Viewcontroller and then run the method, as in the code below:

    - (void)viewDidLoad {
        [super viewDidLoad];
        GDRequestURL* getData = [[GDRequestURL alloc]init];
        [getData getDataFromURL];
        // Aqui instancio um objeto para receber o retorno.
        NSMutableArray* restultado = [[NSMutableArray alloc]init];

        //NEste ponto faço a execução para receber o retorno dos dados.
        restultado = [getData retornaResultado];
  }

But the return is nil.

  • Without seeing the class GDRequestURL I can’t tell what’s going on.

  • @Tiagoamara, create a code answer to your own question.

1 answer

1


I managed to solve!! The problem was at the time that I was performing the return of the data to the variable result. I was doing this before getData performed the data request. Then I created a global variable of the Gdrequesturl class in Viewcontronller, and created an action button to be used for testing, and run the action of returning the data after the completion of the request. Thus remaining:

Viewcontroller. h

...

@interface ViewController{

GDRequestURL* getData;

}

Viewcontroller. m

    ...
     - (void)viewDidLoad {
            [super viewDidLoad];


            getData = [[GDRequestURL alloc]init];
            [getData getDataFromURL];

      }

...

-(IBAction)retornaValores{

         // Aqui instancio um objeto para receber o retorno.
            NSMutableArray* restultado = [[NSMutableArray alloc]init];

            //NEste ponto faço a execução para receber o retorno dos dados.
            restultado = [getData retornaResultado];

...

}
  • Your reply makes no sense. The scope of the method in which a message is sent should not change the correctness of the program. Show your class GDRequestURL so that we can improve it using asynchronous requests.

  • You touched the focus of the problem! As the request already occurs asynchronously I was instantiating and executing the method at the class initialization, and this would cause the sending of the message to occur before the end of the data request, returning an empty object. When I instantiate the object outside the boot, and making it a general scope, it can send the message at any time. I also put a method to allow the object to be returned, only after the request is completed.

  • Yes, if it were synchronous your question code would be working, what I am saying is that it is necessary to add a means to notify your class that the request is over. You can use a Completion Handler in the request or else adopt a formal protocol as a Delegate. This mechanism should be added in GDRequestURL if you’re not present, so I asked to see you.

  • Oh yes, but ta ok now... I created a mechanism to check this. But within what was asked this change worked. That’s why I explained at the beginning of the answer: "...I was making the return of the data to the variable result. I was doing this before getData performed the data request.".

  • I’m glad it worked out, so :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.