Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Managing Network Callbacks #50

Open
nesquena opened this issue Oct 19, 2014 · 0 comments
Open

Managing Network Callbacks #50

nesquena opened this issue Oct 19, 2014 · 0 comments

Comments

@nesquena
Copy link
Member

Guide on managing callback complexity

Could you please send me some code example for structuring a chain of callbacks?

For example using includes to join data or ParseRelation to load associations.

Also, move the callbacks into an object with each request in a method and then use an interface to send out the requests (pseudo-code):

class ObjectFetcher {
    public interface FetcherCallbacks {
         void onFirstDataLoaded(Model impartialData);
         void onAllDataLoaded(Model data);
    }

    FetcherCallbacks callback;
    int requestsLoadedFlag = 0;

    public ObjectFetcher(FetcherCallbacks callback) {
     this.callback = callback;
    }

    public void loadAll() {
         sendFirstRequest();
         sendSecondRequest();
         sendThirdRequest(); 
    }

    private void sendFirstRequest() {
          object.findAllInBackground(new Parse {
               void onDone(Error error) {
                    requestsLoadedFlag +=1;
                     // STORE DATA IN MEMBER VARIABLE HERE
                    callback.onFirstDataLoaded(data);
                    sendFinalDataWhenReady();
               }
          });
    }

    private void sendSecondRequest() {
          object.findAllInBackground(new Parse {
               void onDone(Error error) {
                    requestsLoadedFlag +=1;
                    // STORE DATA IN MEMBER VARIABLE HERE
                    sendFinalDataWhenReady();
               }
          });
    }

   private void sendThirdRequest {
       object.findAllInBackground(new Parse {
               void onDone(Error error) {
                    requestsLoadedFlag +=1;
                    // STORE DATA IN MEMBER VARIABLE HERE
                    sendFinalDataWhenReady();
               }
        }
   } 

   private void sendFinalDataWhenReady() {
      if (requestsLoadedFlag == 3) {
          callback.onAllDataLoaded(data1, data2, data3);
      } 
   }
}

and then use with something like:

ObjectFetcher fetcher = new ObjectFetcher(new FetcherCallbacks() {
     void onFirstDataLoaded {
          // … callback fired here
     }

    void onAllDataLoaded {
          // … callback fired here
     }
})

Note that this is psuedocode but hope it gives you an idea of how to manage things using objects and interface callbacks.

@nesquena nesquena modified the milestone: Topic Work Dec 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant