Recently doing a simple mobile work scheduler, where the data section uses the data storage capabilities provided by Leancloud. Sort out some of the code and share it with you.
Avobject Add a record
Some of the code looks like this:
Private StaticFinal String TABLENAME ="Project";//Create a new record with the database table named Project Public Static void CreateProject(String name) {Final AvobjectObject=NewAvobject (TABLENAME);Object. put ("Name", name);Object. put ("CreateDate",NewDate (). GetTime ());Object. put ("CreateAccount", User.getusername ());Object. put ("Createname", User.getstring ("Name"));Object. put ("Membernum",0);Object. put ("Tasknum",0);Object. put ("State",1); Savecallback callback =NewSavecallback () {@Override Public void Done(Avexception e) {PostInfo (Object, e); } }; Saveproject (Object, callback); }
- The record appears in the project table
- Savecallback is the asynchronous callback after a successful save, and when the E in the done (avexception e) is null, the Save succeeds
Query a record
/*** * 根据id查询记录 */ privatestaticvoidgetProject(String id, GetCallback callback) { new AVQuery<AVObject>(TABLENAME); query.getInBackground(id, callback); }
- Getcallback is an asynchronous callback after a successful query, the method to be implemented is done (Avobject Avobject, avexception e)
Querying more than one record
/*** * get multiple records, sort by creation time */ public static void getallproject () {avquery<avobject> query = new avquery<> (TABLENAME); Query.orderbydescending ( "createat" ); Query.findinbackground (new findcallback<avobject> () { @Override public void Span class= "Hljs-title" >done (list<avobject> list, avexception e) {postinfolist (list, E); } }); }
- Findcallback for asynchronous callbacks after successful query, the method to be implemented is done (list< avobject > List, avexception E)
Modify a single record
Public Static void Addtasknum(String ID) {Getcallback Getcallback =NewGetcallback<avobject> () {@Override Public void Done(FinalAvobject Avobject, avexception e) {if(E = =NULL) {Avobject.put ("Tasknum", Avobject.getint ("Tasknum") +1); Savecallback Savecallback =NewSavecallback () {@Override Public void Done(Avexception e) {if(E! =NULL) {//Exception handling} } };//Update to get the latest valueAvobject.setfetchwhensave (true); Avobject.saveinbackground (Savecallback); }Else{//Exception handling} } }; Getproject (ID, getcallback); }
- Get a single Record object based on ID query first
- Updates a numeric value of the object, which is then saved, savecallback the asynchronous callback after the save succeeds
- Avobject.setfetchwhensave (true); indicates that when the update succeeds, the individual fields of the Avobject are the newest values
Avuser
In addition to the normal object Avobject, the Avuser object is provided to implement a simple user system, and the additional functionality is as follows
Registered
/*** * Create a user account */ pu Blic static void createuser (final string account, final String Password, String Email, String name) {final avuser user = new Avuser (); User.setusername (account); User.setpassword (password); User.setemail (Email); User.put ( "name" , name); User.signupinbackground (new signupcallback () {pub Lic void done (avexception e) { //Logic Processing }}); }
- Avuser supported information in addition to custom fields, there are username (account number), Password (password) and email (email)
- Signupcallback an asynchronous callback operation after a successful registration
Login
/*** * 登录 */ publicstaticvoidLogin(String account, String password) { new LogInCallback<AVUser>() { @Override publicvoiddone(AVUser user, AVException e) { //逻辑处理 } }); }
- Logincallback asynchronous callback for login success
- account, password, respectively, corresponding to the user's username and password
Gets the currently logged on user object
/*** * 得到当前用户 * * @return 当前用户 */ publicstaticgetCurrentUser() { AVUser currentUser = AVUser.getCurrentUser(); return currentUser; }
Cancellation
publicstaticvoidlogOut() { AVUser.logOut(); }
Change Password
/*** * 修改密码 */ publicstaticvoidchangePwd(String old_password, String new_password) { AVUser user = getCurrentUser(); ifnull) { new UpdatePasswordCallback() { @Override publicvoiddone(AVException e) { //逻辑处理 } }); } }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Getting started with leancloud-data storage