[Parse]Bước cơ bản làm việc với Parse

Chúng tôi rất vui mừng được chia sẻ kiến thức sâu sắc về từ khóa Parse la gi để tối ưu hóa nội dung trang web và tiếp thị trực tuyến. Bài viết cung cấp phương pháp tìm kiếm, phân tích từ khóa và chiến lược hiệu quả. Cảm ơn sự quan tâm và hãy tiếp tục theo dõi để cập nhật kiến thức mới.

Theo phương thức truyền thống thì khi lập trình viên mobile khi tạo ra 1 ứng dụng mà cần liên quan đến tài liệu thì sẽ cần phải kèm theo một web developer để tạo 1 backend để sở hữu thể kết nối đến cơ sơ tài liệu cũng như thao tác làm việc trên nó. Như vậy các lập trình viên di động sẽ cần phải đợi chờ backend và viết ra những service phục vụ cho công việc yêu cầu. Nếu cứ theo mô hình như vậy khi làm gì rồi cũng phải phụ thuộc vào web develop và khi cần chỉnh sửa gì thì chỉ có đợi backend và thỉnh thoảng việc backend trả giá trị ra sao thì chỉ biết như vậy. Điều này khiến cho những lập trình viên mobile khó linh động và luôn phải mong chờ. Điều này giờ đã được xử lý bằng phương pháp các lập trình viên mobile sẽ thao tác làm việc trực tiếp sang 1 serice trung gian chuyên xử lý dự liệu là Parse. Trong nội dung bài viết này tôi xin hướng dẫn các bạn một cách cơ bản làm quen với việc sử dụng Parse cho việc lập trình cho mobile (Android) và Backend (Php).

Bạn Đang Xem: [Parse]Bước cơ bản làm việc với Parse

Trước lúc đi vào chi tiết cụ thể tôi xin giới thiệu qua cho những bạn Parse là gì và tương trợ gì cho tất cả chúng ta. Chúng ta cũng có thể vào trực tiếp trang chủ của Parse để sở hữu thể tìm hiểu cũng như đăng ký cho mình 1 tài khoản hoàn toàn miễn phí. Khi vào trang chủ của Parse tất cả chúng ta có thể thấy Parse cung cấp cho tất cả chúng ta 3 dịch vụ chính đó là:

  1. Xử lý tài liệu: Sẽ cung cấp cho ứng dụng của bạn lưu trữ tài liệu, thao tác làm việc dễ dàng với cơ sở tài liệu song song có thể dễ dàng trao đổi tài liệu từ server đến Parse.
  2. Push notification: Đây là 1 trong dịch vụ rất tiện lợi cho những nhà phát triển mobile vì Parse cung cấp sẵn việc Push notification một cách tiện lợi và vô cùng đơn giản (Tôi sẽ nói rõ hơn khi hướng dẫn thao tác làm việc với Android).
  3. Thống kê: Đây là một kênh dịch vụ để nhà phát triển nắm bắt được sự phát triển của ứng dụng mình làm ra để sở hữu những phương án phát triển tiếp theo.

Một điều đáng nói nữa là Parse cung cấp cho lập trình viên bộ thư viện đầy đủ cho gần như toàn bộ các tiếng nói lập trình cho Mobile (iOs, Android, Windows Phone, Unity, Xamarin, ..), cho web hay ứng dụng desktop (OsX, Windows, Php, Unity,..). Thực sự mọi người lập trình viên đều phải sở hữu thể thao tác làm việc với Parse vì được cung cấp tài liệu hướng dẫn đầy đủ cho từng tiếng nói lập trình tại đây.

Để bắt tay vào làm quen với Parse tôi sẽ đưa ra 1 yêu cầu khá đơn giản đó là sẽ tạo ra 1 Object là Blog gồm các trường là nameauthorcontent. Công việc yêu cầu phía backend (Php) thì hãy quản trị được những Blog này và khi thêm mới sẽ gửi 1 thông tin về các client về Blog mới được thêm này. Còn về phía Client (Android App) sẽ hiển thị các Blog và hiện thông báo và click vào đó thì sẽ ra list các Blog gồm cả Blog mới được thêm.

Tạo app mới từ Parse

Sau thời điểm đăng ký mới 1 tài khoản tất cả chúng ta đã khai mạc có thể sử dụng Parse cho công việc của mình, trước tiên ta cần tạo 1 app mới. Sau thời điểm App mới được tạo ra thì tại màn hình hiển thị quản lý App ta vào phần Setting rồi vào phần Keys.

Tại đây App sẽ sinh ra các Key cho mục tiêu thao tác làm việc của lập trình. Ví dụ như Backend Php sẽ cần đến Application Id, REST API KeyMaster Key, Android thì hãy Application IDClinet Key

Backend

Để tùy chỉnh thiết lập và sử dụng Parse cho Php ta cần phải cài Composer trước sau đó tạo 1 file là composer.json có nội dung như sau:

{ “require”: { “parse/php-sdk” : “1.1.*” } }

Sau đó tải về Parse bằng lệnh composer install . Sau thời điểm chạy xong thì ta sẽ sở hữu được thư viện Parse trong thư mục Vendor. Và ở đây để thao tác làm việc với Parse, tôi tạo thêm một file mang tên là parse.php trong thư mục vendor vừa sinh ra. Nội dung file đó như sau

<?php require “autoload.php”; //Load toàn bộ thư viện và các hàm của Parse use ParseParseObject; use ParseParseQuery; use ParseParseACL; use ParseParsePush; use ParseParseUser; use ParseParseInstallation; use ParseParseException; use ParseParseAnalytics; use ParseParseFile; use ParseParseCloud; use ParseParseClient; class Parse { //Để chạy được Parse thì hãy 3 thông số được nhập ở tại chỗ này là app_id, rest_id và master_id private $_app_id = ”; private $_rest_key = ”; private $_master_key = ”; private $_object_name = ”; //Khởi tạo class với tên của Object, tên Object có thể để trống public function __construct($sObject = ”) { $this->_object_name = $sObject; $this->init(); } //Hàm khởi tạo Parse public function init() { ParseClient::initialize($this->_app_id, $this->_rest_key, $this->_master_key); } /** Hàm gửi push notification đến mọi máy di động có sử dụng đến Parse $sMessage: Là nội dung thông tin gửi tới các máy client. Parse có cung cấp cho những người dùng gửi push notification đến những người dân dùng chọn lựa và có 2 cách chọn lựa là Chanel và Advance Targeting. Ở đây là dùng cách Advancer Targetting với việc gửi cho tất cả những máy */ public function sendPushNotification($sMessage) { $data = array(“alert” => $sMessage); $this->init(); $query = ParseInstallation::query(); ParsePush::send(array( “where” => $query, “data” => $data )); } /** Hàm thêm tài liệu vào object, tài liệu truyền vào là các mảng với key là filed và value là giá trị còn đưa vào Hàm trả về id của object do Parse sinh ra */ public function add($aVals = []) { $oObject = new ParseObject($this->_object_name); foreach ($aVals as $sField => $val) { $oObject->set($sField, $val); } try { $oObject->save(); return $oObject->getObjectId(); } catch (ParseException $ex) { return false; } } /** Hàm lấy tất cả tài liệu của một Object */ public function getList() { $query = new ParseQuery($this->_object_name); $results = $query->find(); return $results; } /** Hàm lấy toàn bộ thông tin của một Item vừa vào Id của Parse đã cung cấp lúc thêm mới Hàm trả về là 1 trong Object */ public function getItemById($sObjectId) { $query = new ParseQuery($this->_object_name); return $query->get($sObjectId); } /** Hàm update tài liệu vào object, tài liệu truyền vào là các mảng với key là filed và value là giá trị còn đưa vào Hàm trả về item với thông tin mới update */ public function update($aVals, $sObjectId) { if ($oObject = $this->getItemById($sObjectId)) { foreach ($aVals as $sField => $val) { $oObject->set($sField, $val); } $oObject->save(); } return $oObject->fetch(); } /** Xóa 1 item từ database với id của Parse */ public function delete($sObjectId) { if ($oObject = $this->getItemById($sObjectId)) { $oObject->destroy(); } return true; } }

Giờ khi cần thao tác làm việc với Parse ta chỉ có thêm dòng require “vendor/parse.php”;. Theo yêu cầu của đề bài đưa ra thì tất cả chúng ta sẽ cần 2 trang là màn hình hiển thị list các Blog và xóa từng Blog và màn hình hiển thị thứ hai cho việc thêm mới, sửa xóa Blog. Tương ứng với 2 trang này tôi tạo ra 2 file index.php và add.php.

File index.php

Xem Thêm : Thuật Ngữ Ad Ap Là Gì Trong Game Thủ Nên Biết, Tổng Hợp Thuật Ngữ Trong Liên Quân Mobile

Trang này sẽ thực hiện 2 chức năng là hiển thị toàn bộ Blog đã được thêm và, với mỗi Blog sẽ sở hữu được 2 đường link để sửa và xóa từng Blog, và thêm một đường link để thêm mới Blog. Để xóa 1 Blog thì sẽ cần gửi đến link của file này với biến là delete có mức giá trị là id của Blog này lấy từ tài liệu trả về từ Parse và sau thời điểm xóa xong thì hiển thị list mới. Tương tự link để update Blog này sẽ sang file app.php với id là Id của Blog này.

Tại đây ta có thể mỗi Item mà Parse trả về đều ở dạng Object và muốn lấy các trường trong đấy ta dùng đến hàm get(tên trường), tuy nhiên có 3 hàm đặc biệt quan trọng riêng của Parse đó là

  • Hàm getObjectId() là để lấy id của Item.
  • Hàm getUpdatedAt() lấy thời khắc update sớm nhất có thể.
  • Hàm getCreatedAt() lấy thời kì tạo ra Item.

<?php //Load parse class require “vendor/parse.php”; //Khởi tạo Object Blog $oBlogs = new Parse(‘Blog’); //Kiểm tra xem có truyền biến delete hay là không và lưu giá trị này vào biến $iDeleteId if (isset($_GET[‘delete’]) && $iDeleteId = $_GET[‘delete’]) { //Xóa Blog khỏi database $oBlogs->delete($iDeleteId); //Quay trở về trang index.php header(‘location: index.php’); } $aBlogs = $oBlogs->getList(); ?> <!DOCTYPE htmlvàgt; <htmlvàgt; <headvàgt; <titlevàgt;Parse samplevàlt;/titlevàgt; </headvàgt; <bodyvàgt; <h1vàgt;Parse Sample – Viblovàlt;/h1vàgt; <ulvàgt; <?php foreach($aBlogs as $aBlog) { ?> <livàgt; <?php echo $aBlog->get(‘name’);?> <a href=”add.php?id=<?php echo $aBlog->getObjectId() ?>”>Editvàlt;/avàgt; <a href=”?delete=<?php echo $aBlog->getObjectId() ?>”>Deletevàlt;/avàgt; </livàgt; <?php } ?> </ulvàgt; <a href=”add.php”>Addvàlt;/avàgt; </bodyvàgt; </htmlvàgt;

File add.php

Trang này thực hiện chức năng thêm mới và sửa một Blog. Sự khác nhau giữa thêm mới và sửa là lúc gửi đi sẽ sở hữu được thêm biến Id trên tuyến phố dẫn gửi đến file này. Và sau thời điểm thêm mới thành công sẽ thêm một push notification với nội dung thông tin có một Blog mới và mang tên của Blog. Và sau thời điểm thực hiện thì chuyển về trang index.php

<?php require “vendor/parse.php”; //Load class Parse $oBlog = new Parse(‘Blog’); // Kiểm tra xem có biến Id gửi đến không. if (isset($_GET[‘id’]) && $iId = $_GET[‘id’]) { //Lấy thông tin của một Blog theo Id gửi đến $aBlog = $oBlog->getItemById($iId); } //Kiểm tra xem có phải gửi có gửi tài liệu từ Form lên không? if ($aVals = $_POST) { //Kiểm tra xem có phải đang chỉnh sửa if (isset($iId)) { //Update BLog $oBlog->update($aVals, $aVals[‘id’]); } else { //Thêm mới BLog $oBlog->add($aVals); //Gửi pushs notification $oBlog->sendPushNotification(‘Parse have new blog “‘ . $aVals[‘name’] . ‘”‘); } //Chuyển về trang index.php header(‘location: index.php’); } ?> <!DOCTYPE htmlvàgt; <htmlvàgt; <headvàgt; <titlevàgt;Parse samplevàlt;/titlevàgt; </headvàgt; <bodyvàgt; <h1vàgt;Parse Sample – Viblovàlt;/h1vàgt; <form method=”POST”> <tablevàgt; <trvàgt; <tdvàgt;Name :</tdvàgt; <tdvàgt; <input name=”name” value=”<?php if (isset($aBlog)) echo $aBlog->name;?>”> <?php if (isset($aBlog)) { ?> <input type=”hidden” name=”id” value=”<?php echo $aBlog->getObjectId(); ?>”> <?php } ?> </tdvàgt; </trvàgt; <trvàgt; <tdvàgt;Author :</tdvàgt; <tdvàgt;<input name=”author”value=”<?php if (isset($aBlog)) echo $aBlog->author;?>”></tdvàgt; </trvàgt; <trvàgt; <tdvàgt;Content :</tdvàgt; <tdvàgt;<input name=”content”value=”<?php if (isset($aBlog)) echo $aBlog->content;?>”></tdvàgt; </trvàgt; <trvàgt; <td colspan=”2″> <input type=”submit” value=”Submit”> </tdvàgt; </trvàgt; </tablevàgt; </formvàgt; </bodyvàgt; </htmlvàgt;

Client

Trong bài này, tất cả chúng ta sẽ viết app cho Android sử dụng Android Studio với SDK version 19 tương đương với Android 4.4. Để sử dụng thư viện của Parse thì hãy thêm vào file guild.gradle đoạn code sau:

compile ‘com.parse.bolts:bolts-android:1.+’ compile ‘com.parse:parse-android:1.+’

Sau thời điểm thêm dòng đó thì để kết nối được đến Parse thì bạn cần phải chạy đoạn code này:

Parse.initialize(this, “{app_id}”, “{client_id}”); ParseInstallation.getCurrentInstallation().saveInBackground();

Và đoạn code này tất cả chúng ta cần đưa vào file Application của Android vì Plugin Parse chỉ có được load 1 lần duy nhất. Và để load duy nhất 1 lần tất cả chúng ta cần tạo 1 file và extends đến class Application của Android và trong file AndroidManifest.xml khai báo thêm tên file class đó. Cụ thể ta sẽ tạo 1 file là ParseApplication.java với nội dung như sau:

package com.example.framgianguyenquanghuy.parsesample; import android.app.Application; import com.parse.Parse; import com.parse.ParseInstallation; /** * Created by FRAMGIAnguyen.quang quẻ.huy on 26/11/2015. */ public class ParseApplication extends Application { @Override public void onCreate() { super.onCreate(); Parse.initialize(this, “{app_id}”, “{client_id}”); ParseInstallation.getCurrentInstallation().saveInBackground(); } }

File AndroidManifest.xml

Đây là file thiết lập các cấu hình cho app. Như đã nói ở trên thì ta cần phải khai báo thêm một tính chất để chỉ load Plugin Parse một lần khi chạy app, ta cần thêm tính chất name trong thẻ application. Và cũng để chạy được chức năng push notification thì theo tài liệu này thì ta cần làm các bước sau: Thêm đoạn tại chỗ này trong thẻ application:

<service android:name=”com.parse.PushService” /> <receiver android:name=”com.parse.ParsePushBroadcastReceiver” android:exported=”false”> <intent-filtervàgt; <action android:name=”com.parse.push.intent.RECEIVE” /> <action android:name=”com.parse.push.intent.DELETE” /> <action android:name=”com.parse.push.intent.OPEN” /> </intent-filtervàgt; </receivervàgt; <receiver android:name=”com.parse.GcmBroadcastReceiver” android:permission=”com.google.android.c2dm.permission.SEND”> <intent-filtervàgt; <action android:name=”com.google.android.c2dm.intent.RECEIVE” /> <action android:name=”com.google.android.c2dm.intent.REGISTRATION” /> <category android:name=”com.parse.starter” /> </intent-filtervàgt; </receivervàgt;

Xem Thêm : Bị xì hơi nhiều là do đâu làm sao để khắc phục?

và thay đổi tính chất android:name trong thẻ category bằng tên của app. Tức là thay tên của app vào đoaạn <category android:name=”com.parse.starter” />.

Để chạy được push notification cho app thì cũng cần được phải thêm bên phía ngoài thẻ application như sau:

<uses-permission android:name=”android.permission.INTERNET” /> <uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” /> <uses-permission android:name=”android.permission.WAKE_LOCK” /> <uses-permission android:name=”android.permission.VIBRATE” /> <uses-permission android:name=”android.permission.GET_ACCOUNTS” /> <uses-permission android:name=”com.google.android.c2dm.permission.RECEIVE” /> <permission android:protectionLevel=”signature” android:name=”com.parse.starter.permission.C2D_MESSAGE” /> <uses-permission android:name=”com.parse.starter.permission.C2D_MESSAGE” />

và cũng thay đổi tính chất android:name cho 2 thẻ dưới thành tên của app. Như vậy nội dung của file AndroidManifest.xml sẽ như sau:

<?xml version=”1.0″ encoding=”utf-8″?> <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”com.example.framgianguyenquanghuy.parsesample” > <uses-permission android:name=”android.permission.INTERNET” /> <uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” /> <uses-permission android:name=”android.permission.WAKE_LOCK” /> <uses-permission android:name=”android.permission.VIBRATE” /> <uses-permission android:name=”android.permission.GET_ACCOUNTS” /> <uses-permission android:name=”com.google.android.c2dm.permission.RECEIVE” /> <permission android:protectionLevel=”signature” android:name=”com.example.framgianguyenquanghuy.parsesample.permission.C2D_MESSAGE” /> <uses-permission android:name=”com.example.framgianguyenquanghuy.parsesample.permission.C2D_MESSAGE” /> <application android:allowBackup=”true” android:icon=”@mipmap/ic_launcher” android:label=”@string/app_name” android:supportsRtl=”true” android:theme=”@style/AppTheme” android:name=”.ParseApplication”> <activity android:name=”.MainActivity” android:label=”@string/app_name” android:theme=”@style/AppTheme.NoActionBar” > <intent-filtervàgt; <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filtervàgt; </activityvàgt; <service android:name=”com.parse.PushService” /> <receiver android:name=”com.parse.ParsePushBroadcastReceiver” android:exported=”false”> <intent-filtervàgt; <action android:name=”com.parse.push.intent.RECEIVE” /> <action android:name=”com.parse.push.intent.DELETE” /> <action android:name=”com.parse.push.intent.OPEN” /> </intent-filtervàgt; </receivervàgt; <receiver android:name=”com.parse.GcmBroadcastReceiver” android:permission=”com.google.android.c2dm.permission.SEND”> <intent-filtervàgt; <action android:name=”com.google.android.c2dm.intent.RECEIVE” /> <action android:name=”com.google.android.c2dm.intent.REGISTRATION” /> <category android:name=”com.example.framgianguyenquanghuy.parsesample” /> </intent-filtervàgt; </receivervàgt; </applicationvàgt; </manifestvàgt;

Sau thời điểm khai báo xong tất cả chúng ta có thể thử test push notification tại website quản lý app của Parse.

File Blog.java

Để thao tác làm việc với Blog ta cần khai báo 1 class để lưu trữ các tính chất của Blog đó. Nội dung file Blog như sau

package com.example.framgianguyenquanghuy.parsesample; /** * Created by FRAMGIAnguyen.quang quẻ.huy on 26/11/2015. */ public class Blog { public Blog(String _name, String _author, String _content) { this._name = _name; this._author = _author; this._content = _content; } public String _name; public String get_name() { return _name; } public void set_name(String _name) { this._name = _name; } public String _author; public String get_author() { return _author; } public void set_author(String _author) { this._author = _author; } public String _content; public String get_content() { return _content; } public void set_content(String _content) { this._content = _content; } }

Các file trình bày XML

Theo đề bài ta đưa ra thì ở app sẽ hiển thị list các Blog và sẽ hiển thị dưới dạng List View. Và trong app này còn có 3 file ta cần làm đó là activity_main.xml, content_mail.xml và item.xml

<?xml version=”1.0″ encoding=”utf-8″?> <android.support.design.widget.CoordinatorLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:app=”http://schemas.android.com/apk/res-auto” xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent” android:layout_height=”match_parent” android:fitsSystemWindows=”true” tools:context=”.MainActivity”> <android.support.design.widget.AppBarLayout android:layout_height=”wrap_content” android:layout_width=”match_parent” android:theme=”@style/AppTheme.AppBarOverlay”> <android.support.v7.widget.Toolbar android:id=”@+id/toolbar” android:layout_width=”match_parent” android:layout_height=”?attr/actionBarSize” android:background=”?attr/colorPrimary” app:popupTheme=”@style/AppTheme.PopupOverlay” /> </android.support.design.widget.AppBarLayoutvàgt; <include layout=”@layout/content_main” /> <android.support.design.widget.FloatingActionButton android:id=”@+id/fab” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_gravity=”bottom|end” android:layout_margin=”@dimen/fab_margin” android:src=”@android:drawable/ic_dialog_email” /> </android.support.design.widget.CoordinatorLayoutvàgt; <?xml version=”1.0″ encoding=”utf-8″?> <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” xmlns:app=”http://schemas.android.com/apk/res-auto” android:layout_width=”match_parent” android:layout_height=”match_parent” android:paddingLeft=”@dimen/activity_horizontal_margin” android:paddingRight=”@dimen/activity_horizontal_margin” android:paddingTop=”@dimen/activity_vertical_margin” android:paddingBottom=”@dimen/activity_vertical_margin” app:layout_behavior=”@string/appbar_scrolling_view_behavior” tools:showIn=”@layout/activity_main” tools:context=”.MainActivity”> <ListView android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:id=”@+id/listBlogs” android:layout_alignParentTop=”true” android:layout_alignParentStart=”true” /> </RelativeLayoutvàgt; <?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”match_parent” android:layout_height=”match_parent” android:background=”@drawable/blog_item”> <TextView android:id=”@+id/name” android:layout_width=”fill_parent” android:layout_height=”match_parent” /> <TextView android:id=”@+id/author” android:layout_width=”fill_parent” android:layout_height=”match_parent” /> <TextView android:id=”@+id/content” android:layout_width=”fill_parent” android:layout_height=”match_parent” /> </LinearLayoutvàgt;

File MainActivity.java

Đây là file xử lý tài liệu giống như lớp Controller. Và ở đây khi gọi đến file này sẽ gọi tất cả Blog ra sau đó điền vào adapter là list các Blog được khai báo trong file Blog.java và hiển thị ra màn hình hiển thị kết quả.

package com.example.framgianguyenquanghuy.parsesample; import android.content.Context; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.Thực đơn; import android.view.MenuItem; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import com.parse.ParseQuery; import com.parse.ParseObject; import com.parse.FindCallback; import com.parse.ParseException; import com.parse.PushService; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayListvàlt;Blogvàgt; aBlogs = new ArrayListvàlt;Blogvàgt;(); final BlogAdapter adapter = new BlogAdapter(this, aBlogs); ParseQueryvàlt;ParseObjectvàgt; query = ParseQuery.getQuery(“Blog”); query.findInBackground(new FindCallbackvàlt;ParseObjectvàgt;() { public void done(Listvàlt;ParseObjectvàgt; scoreList, ParseException e) { if (e == null) { for (int i = 0; i < scoreList.size(); i++) { ParseObject item = scoreList.get(i); Blog aItem = new Blog(item.getString(“name”), item.getString(“author”), item.getString(“content”)); adapter.add(aItem); } } else { Log.d(“score”, “Error: ” + e.getMessage()); } } }); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ListView listView = (ListView) findViewById(R.id.listBlogs); listView.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Thực đơn thực đơn) { // Inflate the thực đơn; this adds items to the action bar if it is present. getMenuInflater().inflate(R.thực đơn.menu_main, thực đơn); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the trang chủ/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public class BlogAdapter extends ArrayAdaptervàlt;Blogvàgt; { public BlogAdapter(Context context, ArrayListvàlt;Blogvàgt; blogs) { super(context, 0, blogs); } @Override public View getView(int position, View convertView, ViewGroup parent) { // Get the data item for this position Blog blog = getItem(position); // Check if an existing view is being reused, otherwise inflate the view if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false); } // Lookup view for data population TextView tvName = (TextView) convertView.findViewById(R.id.name); TextView tvHome = (TextView) convertView.findViewById(R.id.author); // Populate the data into the template view using the data object tvName.setText(blog.get_name()); tvHome.setText(blog.get_author()); // Return the completed view to render on screen return convertView; } } }

Kết

Trên đây chỉ là 1 trong bài ứng dụng rất đơn giản về kiểu cách thao tác làm việc với Parse để cho thấy được phương pháp hoạt động ra sao cũng như tiện lợi đem lại. Như vậy ta có thể thấy lập trình viên di động sẽ hoàn toàn dữ thế chủ động với việc thao tác làm việc với tài liệu mà không phải mong chờ phía backend. Tuy nhiên có những điều bất lợi khi thao tác làm việc với Parse đó là việc cần phải có sự thống nhất về tài liệu giữa các lập trình viên, vì tài liệu của Parse là dưới dạng No-Sql nên nếu thiếu sự đồng nhất làm cho gây ra nhiều xáo trộn. Tiếp nữa là vì liên kết dến bên thứ 3 do vậy cũng phụ thuộc vào bên Parse nếu có nâng cấp thì app cũng gần như dừng hoạt động. Như vậy, Parse phù hợp cho việc phát triên app mà it thao tác làm việc với cơ sở tài liệu.

You May Also Like

About the Author: v1000