Skip to content

Question in the example/.../repository_user.dart #182

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

Closed
jiangyang5157 opened this issue Jun 1, 2019 · 4 comments
Closed

Question in the example/.../repository_user.dart #182

jiangyang5157 opened this issue Jun 1, 2019 · 4 comments

Comments

@jiangyang5157
Copy link

jiangyang5157 commented Jun 1, 2019

Hi there,
May I ask a few questions,

[master branch] flutter_parse_sdk/example/lib/data/repositories/user/repository_user.dart

@override
  Future<User> createUser(
      String username, String password, String emailAddress) async {
    api.createUser(username, password, emailAddress);

    final User user = await api.createUser(username, password, emailAddress);
    if (user != null) {
      await db.createUser(username, password, emailAddress);
    }

    return user;
  }

Q: Why do api.createUser(username, password, emailAddress); twice?

[master] flutter_parse_sdk/example/lib/data/repositories/user/repository_user.dart

@override
  Future<User> currentUser() => db.currentUser();

Q2: Why not return api.currentUser(), since db.currentUser() alway null?

@phillwiggins
Copy link
Member

phillwiggins commented Jun 1, 2019 via email

@jiangyang5157
Copy link
Author

jiangyang5157 commented Jun 1, 2019

Hi Phillwiggins,

Cool,, thank for the quick response to my question.

Reason of db.currentUser() is null is that the implementation returns null:

class UserProviderDB implements UserProviderContract {
  @override
  Future<User> currentUser() {
    return null;
  }
...
}

However, api.currentUser() is returning ParseUser::currentUser from SharedPreferences

class UserProviderApi implements UserProviderContract {
  @override
  Future<PUser> currentUser() {
    return ParseUser.currentUser();
  }
...
}

I see the idea of let database involve in **Repository is seems to provide a convenient way to access data in offline (without extra network request) for whatever reason (eg: diet daily plan for a specific user).
Therefore, in the UserRepository case, we only need to handle create/destroy/save scenarios in db. More importantly, we only take action on db when api success.

For example when app want remove a user from server locally and online:

class UserRepository implements UserProviderContract {
  @override
  Future<ApiResponse> destroy(PUser user) async {
    ApiResponse response = await api.destroy(user);
    response = await db.destroy(user);
    return response;
  }
...
}

Should be:

ApiResponse response = await api.destroy(user);
if (response.success) {
   await db.destroy(user);
}
return response;

I just saw the sdk few days ago, please kindly correct me if I'm I get the idea wrong..

@phillwiggins
Copy link
Member

phillwiggins commented Jun 1, 2019 via email

@jiangyang5157
Copy link
Author

Thank you for the answer, Phill.
Surething, probably in the nearly future after I get more understanding of how this sdk manage Parse. I would like to re-visit this example to see what I can do. At this moment, I'm still in the stage to make everything works as I expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants