Doug Stevenson
Developer Advocate
ã©ãã。Play ãµã¼ãã¹ã® Task API for Android ã«ã¤ãã¦åãä¸ãã¦ããããã°ã·ãªã¼ãºã®ãã¼ã 3 ã«ãããã。
ãã¼ã 1 ã§ã¯ API ã®åºæ¬ã、
ãã¼ã 2 ã§ã¯æé©ãªãªã¹ãã¼ã®ã¹ã¿ã¤ã«ãé¸æããæ¹æ³ãè¦ã¦ãã¾ãã。ã¤ã¾ã、çããã¯æ¢ã« Firebase API ãçæããã¿ã¹ã¯ãå¹ççã«ä½¿ç¨ã§ããããã«ãªã£ã¦ãã¾ã。ããã、ã¿ã¹ã¯ã®é«åº¦ãªä½¿ç¨æ¹æ³ã«ã¤ãã¦ããã«è¸ã¿è¾¼ãã§ã¿ããæ¹ã¯、ãã²æ¬è¨äºããèªã¿ãã ãã。
Task ã«åãããã
Android åãã® Firebase æ©è½ã«ã¯、ã¿ã¹ã¯ãå®äºããéã«éç¥ãã¦ããããã®ãããã¾ã。ããã、ã¹ã¬ããã§åä½ããç¬èªã¿ã¹ã¯ãä½æãããå ´åã¯ã©ãããã°ããã§ãããã。Task API ã¯、ãããã£ãå ´åã«å½¹ç«ã¤ãã¼ã«ã§ã。ã¢ããªã« Firebase ãçµã¿è¾¼ã¾ãã« Task API ã使ãããå ´åã¯、build.gradle ã«æ¬¡ã®ä¾åæ§ã追å ããã¨、å¿
è¦ãªã©ã¤ãã©ãªãåå¾ã§ãã¾ã。
compile 'com.google.android.gms:play-services-tasks:9.6.1'
ããã、Firebase ã
çµã¿è¾¼ã å ´åã¯、ãã®ã©ã¤ãã©ãªãèªåçã«åå¾ããããã、åå¥ã«æå®ããå¿
è¦ã¯ããã¾ãã。
æ°ãã Task ã®èµ·åã«ä½¿ãã¡ã½ãã㯠1 ã¤ã ãã§ãã、å¼ã³åºãã®æ¹æ³ã¯ 2 éãããã¾ã。ããã«ã¯、Task ã¦ã¼ãã£ãªã㣠ã¯ã©ã¹ã®「call」ã¨ããéçã¡ã½ããã使ãã¾ã。2 ã¤ã®å¼ã³åºãæ¹æ³ã次ã«ç¤ºãã¾ã。
Task<TResult> call(Callable<TResult> callable)
Task<TResult> call(Executor executor, Callable<TResult> callable)
addOnSuccessListener() ã¨åæ§ã«、
call() ã«ãã¡ã¤ã³ã¹ã¬ããä¸ã§å®è¡ãããã®ã¨、ä½æ¥ã Executor ã«éä¿¡ãããã®ãããã¾ã。å®è¡ããä½æ¥ã¯、渡ããã
Callable ã®ä¸ã§æå®ãã¾ã。Java ã® Callable 㯠Runnable ã«ä¼¼ã¦ãã¾ãã、çµæã®åãã©ã¡ã¼ã¿ãæã£ã¦ãã、ãã®åã call() ã¡ã½ãããè¿ããªãã¸ã§ã¯ãã®åã«ãªãã¾ã。ãã®çµæã®åã¯、call() ãè¿ã Task ã®åã«ããªãã¾ã。次ã«ç¤ºãã®ã¯、String ãè¿ãã ãã®åç´ãª Callable ã§ã。
public class CarlyCallable implements Callable<String> {
@Override
public String call() throws Exception {
return "Call me maybe";
}
}
CarlyCallable ã®ãã©ã¡ã¼ã¿ã¯ String ã§ãããã¨ã«æ³¨ç®ãã¦ãã ãã。ããã¯、call() ã¡ã½ããã String ãè¿ãå¿
è¦ããããã¨ã示ãã¦ãã¾ã。ãããã Task ãä½æããå¦ç㯠1 è¡ã§æ¸ããã¨ãã§ãã¾ã。
Task<String> task = Tasks.call(new CarlyCallable());
ãã®è¡ãå®è¡ãããã¨、CarlyCallable ã® call() ã¡ã½ãããã¡ã¤ã³ã¹ã¬ããã§å¼ã³åºãã、(çµæã¯ããããã£ã¦ãã¾ãã)Task ã«ãªã¹ãã¼ã追å ãã¦çµæãæ±ãããã¨ãã§ãã¾ã。ããã«èå³æ·±ã Callable ã¯、å®éã«ãã¼ã¿ãã¼ã¹ããããã¯ã¼ã¯ ã¨ã³ããã¤ã³ããããã¼ã¿ãèªã¿è¾¼ããã®ã§ããã。ãã®ãããªéãå¦çãä¼´ã Callable ã¯、æåã®å¼æ°ã¨ã㦠Executor ã渡ããã¨ãã§ãã
call() ã® 2 çªç®ã®å½¢å¼ã使ã£ã¦ Executor ä¸ã§å®è¡ãããã¨ãã§ãã¾ã。
è¤æ°ã® Task ãç¹ãã
ä¾ã¨ãã¦、CarlyCallable ã® Task ã®çµæã¨ãã¦çæããã String ãå¦çãããã¨ãèãã¦ã¿ã¾ããã。ããã§ã¯、çµæã® String ã®æååèªä½ã«èå³ãããã®ã§ã¯ãªã、æååãåã
ã®åèªã«åå²ã List<String> ã«æ ¼ç´ããããã®ã«èå³ãããã¨ãã¾ã。ããã、CarlyCallable ãå¤æ´ããå¿
è¦ãããã¨ã¯éãã¾ãã。CarlyCallable ã¯æ³å®ãããä½æ¥ãæ£ç¢ºã«è¡ã£ã¦ãã、å¥ã®å ´æã§ãã®ã¾ã¾å©ç¨ã§ããããã§ã。ãã®ãã、CarlyCallable ãè¿ãã String ãããããã®åèªã«åå²ãããã¸ãã¯ãä½æã、CarlyCallable ã®å¦çã®å¾ã«å®è¡ãããã§ã。ããã¯、
Continuation ã使ã£ã¦å®ç¾ã§ãã¾ã。Continuation ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£
㯠1 ã¤ã® Task ã®åºåãåãåã、ããå¦çãè¡ã£ã¦、çµæã®ãªãã¸ã§ã¯ããè¿ãã¾ã。ããã¯åãåã§ãªãã¦ãæ§ãã¾ãã。次ã«ç¤ºãã®ã¯、æååãååèªãã¨ã« String ã® List ã«åå²ãã Continuation ã§ã。
public class SeparateWays implements Continuation<String, List<String>> {
@Override
public List<String> then(Task<String> task) throws Exception {
return Arrays.asList(task.getResult().split(" +"));
}
}
ããã§å®è£
ããã¦ãã Continuation ã¤ã³ã¿ã¼ãã§ã¼ã¹ã«ã¯、å
¥åã®å(String)ã¨åºåã®å(List
)ã® 2 ã¤ã®åãã©ã¡ã¼ã¿ããããã¨ã«æ³¨ç®ãã¦ãã ãã。å
¥åã¨åºåã®åã¯、ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å¯ä¸ã®ã¡ã½ããã§ãã then() ã®ã·ã°ããã£ã«ä½¿ç¨ããã¦ãã、ãã®ã¡ã½ããã§è¡ãããæä½ãå®ç¾©ãã¦ãã¾ã。ç¹ã«æ³¨ç®ããå¿
è¦ãããã®ã¯、then() ã«æ¸¡ããããã©ã¡ã¼ã¿ã® Task ã§ã。ãã® String ã¯、Continuation ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å
¥ååã¨ä¸è´ãã¦ããå¿
è¦ãããã¾ã。ãããããã¨ã«ãã£ã¦ Continuation ã¯å
¥åãå¾ããã¨ãã§ã、å®äºãã Task ããçµæãå¼ãåºããã¨ãã§ãã¾ã。
次ã«ç¤ºãã®ã¯、String ã® List ãç¡ä½çºã«ä¸¦ã¹å¤ããå¥ã® Continuation ã§ã。
public class AllShookUp implements Continuation<List<String>, List<String>> {
@Override
public List<String> then(@NonNull Task<List<String>> task) throws Exception {
// Randomize a copy of the List, not the input List itself, since it could be immutable
final ArrayList<String> shookUp = new ArrayList<>(task.getResult());
Collections.shuffle(shookUp);
return shookUp;
}
}
ããã«、String ã® List ã 1 åã®ã¹ãã¼ã¹ã§åºåã£ã String ã«çµåãããã®ãä½ãã¾ã。
private static class ComeTogether implements Continuation<List<String>, String> {
@Override
public String then(@NonNull Task<List<String>> task) throws Exception {
StringBuilder sb = new StringBuilder();
for (String word : task.getResult()) {
if (sb.length() > 0) {
sb.append(' ');
}
sb.append(word);
}
return sb.toString();
}
}
ä½ããããã¨ãã¦ããã®ãã¯ãããåããããããã¾ãã。以ä¸ã®æä½ãã¾ã¨ãã¦、æåã® Task ã® String ã®åèªã®é çªãç¡ä½çºåã、ãã®çµæããæ°ãã String ãçæããæä½ã®ãã§ã¼ã³ã«ãã¾ã。
Task<String> playlist = Tasks.call(new CarlyCallable())
.continueWith(new SeparateWays())
.continueWith(new AllShookUp())
.continueWith(new ComeTogether());
playlist.addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String message) {
// The final String with all the words randomized is here
}
});
Task ã®
continueWith() ã¡ã½ããã¯、æå®ããã Continuation ã«ãã£ã¦å¦çãããåã® Task ã«å¯¾ããè¨ç®ãè¡ããã¨ã示ãæ°ãã Task ãè¿ãã¾ã。ããã§è¡ã£ã¦ããã®ã¯、continueWith() ã®å¼ã³åºãããã§ã¼ã³ããããã¨ã§ã。ãã®ããã«ãã¦æä½ã®ãã¤ãã©ã¤ã³ãå½¢æã、åã¹ãã¼ã¸ã®å®äºãå¾
ã£ã¦ãã次ã®ã¹ãã¼ã¸ãå®è¡ãããããªåæ Task ãä½æã§ãã¾ã。
ãã®æä½ã®ãã§ã¼ã³ã¯、巨大㪠String ãæ±ãå ´åã«åé¡ã«ãªãããããã¾ãã。ããã§、ãã¹ã¦ã®å¦çãå¥ã®ã¹ã¬ããã§è¡ã、ã¡ã¤ã³ã¹ã¬ããããããã¯ããªãããã«å¤æ´ãã¦ã¿ã¾ããã。
Executor executor = ... // you decide!
Task<String> playlist = Tasks.call(executor, new CarlyCallable())
.continueWith(executor, new SeparateWays())
.continueWith(executor, new AllShookUp())
.continueWith(executor, new ComeTogether());
playlist.addOnSuccessListener(executor, new OnSuccessListener() {
@Override
public void onSuccess(String message) {
// Do something with the output of this playlist!
}
});
ããã§、Callable ã¨ãã¹ã¦ã® Continuation ã¨æå¾ã® Task ãªã¹ãã¼ã¯、
ãã¹ã¦ Executor ã決ããã¹ã¬ããä¸ã§å®è¡ãããããã«ãªã、å¦çã®éã«ã¡ã¤ã³ã¹ã¬ããã UI ãªã©ã®å¦çãè¡ããããã«ãªãã¾ã。ããã§、UI ãããã¤ããã¨ã¯ãªããªãã¾ã。
åãã¦è¦ãã¨ãã¯、ãã¹ã¦ã®æä½ãå¥ã®ã¯ã©ã¹ã«åããã®ã¯ã©ããã¨æãããããã¾ãã。å¿
è¦ãªãã¨ã ããè¡ãæ°è¡ã®ã¡ã½ããã 1 ã¤æ¸ãã ãã§æ¸ã¾ãããã¨ãã§ããã§ããã。ãã ã、ãã®ä¾ã¯、Task ãã©ã®ããã«åä½ãããã«æ³¨ç®ããç°¡åãªä¾ã§ãããã¨ã«ã注æãã¦ãã ãã。Task 㨠Continuation ã®ãã§ã¼ã³ã«ããã¡ãªããã¯、(ããªãåç´ãªæ©è½ã§ããã«ãããããã)次ã®ãããªãã¨ãè¡ãå ´åã«é¡èã«è¡¨ãã¾ã。
å
¥åããã String ãæ°ããã½ã¼ã¹ããåå¾ãããå ´åã¯ã©ããã¾ãã。ãã¨ãã°、BlondieCallable ãPaulSimonCallable ããã£ãå ´åã¯ã©ãã§ããã。
å
¥åããã String ã«å¯¾ãã¦å¥ã®å¦çãè¡ãå ´åã¯ã©ããã¾ãã。ãã¨ãã°、List å
ã® String ã®é çªã(ã¬ã³ã¼ãã®ããã«)å³æ¹åã«å転ããã YouSpinMeRound ã¨ãã Continuation ãè¡ãå ´åã§ã。
å¥ã®ã¹ã¬ããã§å¦çãã¤ãã©ã¤ã³ã®å¥ã®ã³ã³ãã¼ãã³ããå®è¡ãããå ´åã¯ã©ããã¾ãã。
ç¾å®çã«、Task ã® Continuation ã¯、ãã¼ã¿ã»ããã«å¯¾ãã filter、map、reduce æ©è½ã®ä¸é£ã®ã¢ã¸ã¥ã¼ã« ãã§ã¼ã³ãå®è¡ããããã«ä½¿ããã¨ãå¤ãã§ããã。ã¾ã、ã³ã¬ã¯ã·ã§ã³ã大ãããªãå¯è½æ§ãããå ´åã¯、ãããã£ãåä½ãã¡ã¤ã³ã¹ã¬ãã以å¤ã§è¡ãããå ´åãããã¾ã。ããã、ããã§é³æ¥½ã楽ãããã¨ãã§ããã§ããã!
Task ã®ãã§ã¼ã³ã®å®è¡ä¸ã§ã¨ã©ã¼ãçºçããã
æå¾ã«、Continuation ã«ã¤ãã¦ãã 1 ã¤ç¥ã£ã¦ããã¹ããã¨ãããã¾ã。ã©ããã®ã¹ãã¼ã¸ã®å¦çä¸ã«ã©ã³ã¿ã¤ã ä¾å¤ãã¹ãã¼ãããå ´å、é常、ãã®ä¾å¤ã¯ãã§ã¼ã³ã®æå¾ã® Task ã®å¤±æãç£è¦ãããªã¹ãã¼ã¾ã§ä¼æããã¾ã。ä»»æã® Continuation ã§ã¯、å
¥å Task ã«å¯¾ã㦠isSuccessful() ã¡ã½ãããå®è¡ã、ã¿ã¹ã¯ãæåãããã©ããã確èªã§ãã¾ã。ã¾ãã¯、(ä¸è¨ã®ä¾ã§è¡ã£ã¦ããããã«)åã« getResult() ãå¼ã³åºãã¨、以åã«å¤±æãããã°ä¾å¤ãåã¹ãã¼ãã、èªåçã«æ¬¡ã® Continuation ã«ãã©ãçãã¾ã。ãã ã、失æããå¯è½æ§ãããå ´å、ãã§ã¼ã³ã®æå¾ã® Task ã®ãªã¹ãã¼ã§å¿
ã失æã確èªããããã«ãã¾ã。
ãã¨ãã°、ä¸è¨ã®ãã§ã¼ã³ã® CarlyCallable ã null ãè¿ããå ´å、SeparateWays ãè¡ã Continuation ã NullPointerException ãã¹ãã¼ã、ãããæå¾ã® Task ã¾ã§ä¼æãã¾ã。Task ã« OnFailureListener ãç»é²ããã¦ããå ´å、åãä¾å¤ã®ã¤ã³ã¹ã¿ã³ã¹ã«ãã£ã¦ãããå¼ã³åºããã¾ã。
ãããã¯ã¤ãº
å
ã»ã©ã®ãã§ã¼ã³ã§、å¦çã³ã³ãã¼ãã³ããå¤æ´ããã«å
ã®æååã«å«ã¾ããåèªã®æ°ãè¦ã¤ãåºã
æãå¹çç ãªæ¹æ³ã¯ä½ã§ãããã。ç¶ããèªãåã«、å°ãèãã¦ã¿ã¦ãã ãã。
çãã¯、ããããçãããèãããããã·ã³ãã«ãªãã®ã§ã。並ã³é ãç¡ä½çºã«ãªã£ã¦ããã ããªã®ã§、æãããããããã½ãªã¥ã¼ã·ã§ã³ã¯、æçµçãªåºåæååã®åèªã®æ°ãæ°ãããã¨ã§ã。ããã、ããã«ãã 1 ã¤ã®ããªãã¯ãæ½ãã§ãã¾ã。continueWith() ãå¼ã°ãããã³ã«æ°ãã Task ã¤ã³ã¹ã¿ã³ã¹ãè¿ããã¾ãã、ãããã¯ããã§åç
§ãããã¨ãã§ãã¾ãã。ãªããªã、ãã§ã¼ã³æ§æãå©ç¨ãã¦æå¾ã® Task ã«é£çµããã¦ããããã§ã。ãã®ãã、ä»»æã®ã¿ã¹ã¯ãã¤ã³ã¿ã¼ã»ãããã¦、次㮠Continuation ã«å ãã¦、å¥ã®ãªã¹ãã¼ã追å ããã¨ããæ¹æ³ãã¨ãã¾ã。
Task<List<String>> split_task = Tasks.call(new CarlyCallable())
.continueWith(executor, new SeparateWays());
split_task =
.continueWith(executor, new AllShookUp())
.continueWith(executor, new ComeTogether());
split_task.addOnCompleteListener(executor, new OnCompleteListener<List<String>>() {
@Override
public void onComplete(@NonNull Task<List<String>> task) {
// Find the number of words just by checking the size of the List
int size = task.getResult().size();
}
});
playlist.addOnCompleteListener( /* as before... */ );
Task ãå®äºããã¨、
両æ¹ã® Continuation ãå®è¡ãã、ããã¨ã¨ãã«
ãã¹ã¦ã® 追å ããããªã¹ãã¼ãå®è¡ããã¾ã。ããã§è¡ã£ãã®ã¯、SeparateWays ã¨ãã Continuation ã®åºåãåå¾ãã Task ãã¤ã³ã¿ã¼ã»ããã、ãã®åºåãç´æ¥ãªãã¹ã³ããå¦çã追å ããã ãã§ã。Continuation ã®ãã§ã¼ã³ã«ã¯å½±é¿ãã¾ãã。ã¤ã³ã¿ã¼ã»ããããã¿ã¹ã¯ã§ List ã«å¯¾ã㦠size() ãå¼ã³åºãã ãã§、åèªã®æ°ãåå¾ã§ãã¾ã。
ãã®ã·ãªã¼ãºã®ãã¼ã 3 ã®ã¾ã¨ã
ãããããªåè«ã¯ãã¦ãã、Task API ã使ç¨ããã¨、ã·ã¼ã±ã³ã·ã£ã«ãªãã¤ãã©ã¤ã³å¦çãç°¡åã«ã¢ã¸ã¥ã¼ã«å½¢å¼ã§è¡¨ç¾ãã¦å®è¡ãããã¨ãã§ãã¾ã。ããã«、å¦çã®åã¹ãã¼ã¸ã§ã©ã® Executor ã使ãããæå®ã§ãã¾ã。ãã®å¦çã¯、Firebase ãã¢ããªã«çµã¿è¾¼ããçµã¿è¾¼ã¾ãªããã«ããããã、ç¬èªã® Task ã Firebase API ã® Task ãå©ç¨ãã¦å®è¡ã§ãã¾ã。æ¬ã·ãªã¼ãºã®æçµåã¨ãªã次åã§ã¯、Task ã使ã£ã¦è¤æ°ã®ä½æ¥ãåæå®è¡ãã並åå¦çã®èµ·åæ¹æ³ã«ã¤ãã¦è¦ã¦ããã¾ã。
ãã¤ãã®ããã«、質åãããã¾ããã、Twitter ã§ããã·ã¥ã¿ã° #AskFirebase ãæ¤ç´¢ããã、
firebase-talk Google Group ããå©ç¨ãã ãã。å°ç¨ã® Firebase Slack ãã£ãã«ãç¨æãã¦ãã¾ã。Twitter ã§
@CodingDoug ã®ãã©ãã¼ããããããé¡ããã¾ã。ã·ãªã¼ãºã®æ¬¡ã®è¨äºãæ稿ãããéã«ãç¥ãããããã¾ã。
æå¾ã«、ãã®æ稿ã«ç»å ´ãããã¹ã¦ã®
æ² ãä¸ãã¦ããã¾ããã。
Posted by
Khanh LeViet - Developer Relations Team
ãã®è¨äºã¯ Doug Stevenson ã«ãã The Firebase Blog ã®è¨äº "The Firebase Blog: Become a Firebase Taskmaster! (Part 3: Wiring up your Tasks) " ãå
ã«ç¿»è¨³・å çãããã®ã§ã。詳ããã¯å
è¨äºãã覧ãã ãã。
Doug Stevenson
Developer Advocate
ã©ãã。Play ãµã¼ãã¹ã® Task API for Android ã«ã¤ãã¦åãä¸ãã¦ããããã°ã·ãªã¼ãºã®ãã¼ã 3 ã«ãããã。ãã¼ã 1 ã§ã¯ API ã®åºæ¬ã、ãã¼ã 2 ã§ã¯æé©ãªãªã¹ãã¼ã®ã¹ã¿ã¤ã«ãé¸æããæ¹æ³ãè¦ã¦ãã¾ãã。ã¤ã¾ã、çããã¯æ¢ã« Firebase API ãçæããã¿ã¹ã¯ãå¹ççã«ä½¿ç¨ã§ããããã«ãªã£ã¦ãã¾ã。ããã、ã¿ã¹ã¯ã®é«åº¦ãªä½¿ç¨æ¹æ³ã«ã¤ãã¦ããã«è¸ã¿è¾¼ãã§ã¿ããæ¹ã¯、ãã²æ¬è¨äºããèªã¿ãã ãã。
Task ã«åãããã
Android åãã® Firebase æ©è½ã«ã¯、ã¿ã¹ã¯ãå®äºããéã«éç¥ãã¦ããããã®ãããã¾ã。ããã、ã¹ã¬ããã§åä½ããç¬èªã¿ã¹ã¯ãä½æãããå ´åã¯ã©ãããã°ããã§ãããã。Task API ã¯、ãããã£ãå ´åã«å½¹ç«ã¤ãã¼ã«ã§ã。ã¢ããªã« Firebase ãçµã¿è¾¼ã¾ãã« Task API ã使ãããå ´åã¯、build.gradle ã«æ¬¡ã®ä¾åæ§ã追å ããã¨、å¿
è¦ãªã©ã¤ãã©ãªãåå¾ã§ãã¾ã。
compile 'com.google.android.gms:play-services-tasks:9.6.1'
ããã、Firebase ã çµã¿è¾¼ã å ´åã¯、ãã®ã©ã¤ãã©ãªãèªåçã«åå¾ããããã、åå¥ã«æå®ããå¿
è¦ã¯ããã¾ãã。
æ°ãã Task ã®èµ·åã«ä½¿ãã¡ã½ãã㯠1 ã¤ã ãã§ãã、å¼ã³åºãã®æ¹æ³ã¯ 2 éãããã¾ã。ããã«ã¯、Task ã¦ã¼ãã£ãªã㣠ã¯ã©ã¹ã®「call」ã¨ããéçã¡ã½ããã使ãã¾ã。2 ã¤ã®å¼ã³åºãæ¹æ³ã次ã«ç¤ºãã¾ã。
Task<TResult> call(Callable<TResult> callable)
Task<TResult> call(Executor executor, Callable<TResult> callable)
addOnSuccessListener() ã¨åæ§ã«、call() ã«ãã¡ã¤ã³ã¹ã¬ããä¸ã§å®è¡ãããã®ã¨、ä½æ¥ã Executor ã«éä¿¡ãããã®ãããã¾ã。å®è¡ããä½æ¥ã¯、渡ããã Callable ã®ä¸ã§æå®ãã¾ã。Java ã® Callable 㯠Runnable ã«ä¼¼ã¦ãã¾ãã、çµæã®åãã©ã¡ã¼ã¿ãæã£ã¦ãã、ãã®åã call() ã¡ã½ãããè¿ããªãã¸ã§ã¯ãã®åã«ãªãã¾ã。ãã®çµæã®åã¯、call() ãè¿ã Task ã®åã«ããªãã¾ã。次ã«ç¤ºãã®ã¯、String ãè¿ãã ãã®åç´ãª Callable ã§ã。
public class CarlyCallable implements Callable<String> {
@Override
public String call() throws Exception {
return "Call me maybe";
}
}
CarlyCallable ã®ãã©ã¡ã¼ã¿ã¯ String ã§ãããã¨ã«æ³¨ç®ãã¦ãã ãã。ããã¯、call() ã¡ã½ããã String ãè¿ãå¿
è¦ããããã¨ã示ãã¦ãã¾ã。ãããã Task ãä½æããå¦ç㯠1 è¡ã§æ¸ããã¨ãã§ãã¾ã。
Task<String> task = Tasks.call(new CarlyCallable());
ãã®è¡ãå®è¡ãããã¨、CarlyCallable ã® call() ã¡ã½ãããã¡ã¤ã³ã¹ã¬ããã§å¼ã³åºãã、(çµæã¯ããããã£ã¦ãã¾ãã)Task ã«ãªã¹ãã¼ã追å ãã¦çµæãæ±ãããã¨ãã§ãã¾ã。ããã«èå³æ·±ã Callable ã¯、å®éã«ãã¼ã¿ãã¼ã¹ããããã¯ã¼ã¯ ã¨ã³ããã¤ã³ããããã¼ã¿ãèªã¿è¾¼ããã®ã§ããã。ãã®ãããªéãå¦çãä¼´ã Callable ã¯、æåã®å¼æ°ã¨ã㦠Executor ã渡ããã¨ãã§ãã call() ã® 2 çªç®ã®å½¢å¼ã使ã£ã¦ Executor ä¸ã§å®è¡ãããã¨ãã§ãã¾ã。
è¤æ°ã® Task ãç¹ãã
ä¾ã¨ãã¦、CarlyCallable ã® Task ã®çµæã¨ãã¦çæããã String ãå¦çãããã¨ãèãã¦ã¿ã¾ããã。ããã§ã¯、çµæã® String ã®æååèªä½ã«èå³ãããã®ã§ã¯ãªã、æååãåã
ã®åèªã«åå²ã List<String> ã«æ ¼ç´ããããã®ã«èå³ãããã¨ãã¾ã。ããã、CarlyCallable ãå¤æ´ããå¿
è¦ãããã¨ã¯éãã¾ãã。CarlyCallable ã¯æ³å®ãããä½æ¥ãæ£ç¢ºã«è¡ã£ã¦ãã、å¥ã®å ´æã§ãã®ã¾ã¾å©ç¨ã§ããããã§ã。ãã®ãã、CarlyCallable ãè¿ãã String ãããããã®åèªã«åå²ãããã¸ãã¯ãä½æã、CarlyCallable ã®å¦çã®å¾ã«å®è¡ãããã§ã。ããã¯、Continuation ã使ã£ã¦å®ç¾ã§ãã¾ã。Continuation ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£
㯠1 ã¤ã® Task ã®åºåãåãåã、ããå¦çãè¡ã£ã¦、çµæã®ãªãã¸ã§ã¯ããè¿ãã¾ã。ããã¯åãåã§ãªãã¦ãæ§ãã¾ãã。次ã«ç¤ºãã®ã¯、æååãååèªãã¨ã« String ã® List ã«åå²ãã Continuation ã§ã。
public class SeparateWays implements Continuation<String, List<String>> {
@Override
public List<String> then(Task<String> task) throws Exception {
return Arrays.asList(task.getResult().split(" +"));
}
}
ããã§å®è£
ããã¦ãã Continuation ã¤ã³ã¿ã¼ãã§ã¼ã¹ã«ã¯、å
¥åã®å(String)ã¨åºåã®å(List)ã® 2 ã¤ã®åãã©ã¡ã¼ã¿ããããã¨ã«æ³¨ç®ãã¦ãã ãã。å
¥åã¨åºåã®åã¯、ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å¯ä¸ã®ã¡ã½ããã§ãã then() ã®ã·ã°ããã£ã«ä½¿ç¨ããã¦ãã、ãã®ã¡ã½ããã§è¡ãããæä½ãå®ç¾©ãã¦ãã¾ã。ç¹ã«æ³¨ç®ããå¿
è¦ãããã®ã¯、then() ã«æ¸¡ããããã©ã¡ã¼ã¿ã® Task ã§ã。ãã® String ã¯、Continuation ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å
¥ååã¨ä¸è´ãã¦ããå¿
è¦ãããã¾ã。ãããããã¨ã«ãã£ã¦ Continuation ã¯å
¥åãå¾ããã¨ãã§ã、å®äºãã Task ããçµæãå¼ãåºããã¨ãã§ãã¾ã。
次ã«ç¤ºãã®ã¯、String ã® List ãç¡ä½çºã«ä¸¦ã¹å¤ããå¥ã® Continuation ã§ã。
public class AllShookUp implements Continuation<List<String>, List<String>> {
@Override
public List<String> then(@NonNull Task<List<String>> task) throws Exception {
// Randomize a copy of the List, not the input List itself, since it could be immutable
final ArrayList<String> shookUp = new ArrayList<>(task.getResult());
Collections.shuffle(shookUp);
return shookUp;
}
}
ããã«、String ã® List ã 1 åã®ã¹ãã¼ã¹ã§åºåã£ã String ã«çµåãããã®ãä½ãã¾ã。
private static class ComeTogether implements Continuation<List<String>, String> {
@Override
public String then(@NonNull Task<List<String>> task) throws Exception {
StringBuilder sb = new StringBuilder();
for (String word : task.getResult()) {
if (sb.length() > 0) {
sb.append(' ');
}
sb.append(word);
}
return sb.toString();
}
}
ä½ããããã¨ãã¦ããã®ãã¯ãããåããããããã¾ãã。以ä¸ã®æä½ãã¾ã¨ãã¦、æåã® Task ã® String ã®åèªã®é çªãç¡ä½çºåã、ãã®çµæããæ°ãã String ãçæããæä½ã®ãã§ã¼ã³ã«ãã¾ã。
Task<String> playlist = Tasks.call(new CarlyCallable())
.continueWith(new SeparateWays())
.continueWith(new AllShookUp())
.continueWith(new ComeTogether());
playlist.addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String message) {
// The final String with all the words randomized is here
}
});
Task ã® continueWith() ã¡ã½ããã¯、æå®ããã Continuation ã«ãã£ã¦å¦çãããåã® Task ã«å¯¾ããè¨ç®ãè¡ããã¨ã示ãæ°ãã Task ãè¿ãã¾ã。ããã§è¡ã£ã¦ããã®ã¯、continueWith() ã®å¼ã³åºãããã§ã¼ã³ããããã¨ã§ã。ãã®ããã«ãã¦æä½ã®ãã¤ãã©ã¤ã³ãå½¢æã、åã¹ãã¼ã¸ã®å®äºãå¾
ã£ã¦ãã次ã®ã¹ãã¼ã¸ãå®è¡ãããããªåæ Task ãä½æã§ãã¾ã。
ãã®æä½ã®ãã§ã¼ã³ã¯、巨大㪠String ãæ±ãå ´åã«åé¡ã«ãªãããããã¾ãã。ããã§、ãã¹ã¦ã®å¦çãå¥ã®ã¹ã¬ããã§è¡ã、ã¡ã¤ã³ã¹ã¬ããããããã¯ããªãããã«å¤æ´ãã¦ã¿ã¾ããã。
Executor executor = ... // you decide!
Task<String> playlist = Tasks.call(executor, new CarlyCallable())
.continueWith(executor, new SeparateWays())
.continueWith(executor, new AllShookUp())
.continueWith(executor, new ComeTogether());
playlist.addOnSuccessListener(executor, new OnSuccessListener() {
@Override
public void onSuccess(String message) {
// Do something with the output of this playlist!
}
});
ããã§、Callable ã¨ãã¹ã¦ã® Continuation ã¨æå¾ã® Task ãªã¹ãã¼ã¯、 ãã¹ã¦ Executor ã決ããã¹ã¬ããä¸ã§å®è¡ãããããã«ãªã、å¦çã®éã«ã¡ã¤ã³ã¹ã¬ããã UI ãªã©ã®å¦çãè¡ããããã«ãªãã¾ã。ããã§、UI ãããã¤ããã¨ã¯ãªããªãã¾ã。
åãã¦è¦ãã¨ãã¯、ãã¹ã¦ã®æä½ãå¥ã®ã¯ã©ã¹ã«åããã®ã¯ã©ããã¨æãããããã¾ãã。å¿
è¦ãªãã¨ã ããè¡ãæ°è¡ã®ã¡ã½ããã 1 ã¤æ¸ãã ãã§æ¸ã¾ãããã¨ãã§ããã§ããã。ãã ã、ãã®ä¾ã¯、Task ãã©ã®ããã«åä½ãããã«æ³¨ç®ããç°¡åãªä¾ã§ãããã¨ã«ã注æãã¦ãã ãã。Task 㨠Continuation ã®ãã§ã¼ã³ã«ããã¡ãªããã¯、(ããªãåç´ãªæ©è½ã§ããã«ãããããã)次ã®ãããªãã¨ãè¡ãå ´åã«é¡èã«è¡¨ãã¾ã。
å
¥åããã String ãæ°ããã½ã¼ã¹ããåå¾ãããå ´åã¯ã©ããã¾ãã。ãã¨ãã°、BlondieCallable ãPaulSimonCallable ããã£ãå ´åã¯ã©ãã§ããã。
å
¥åããã String ã«å¯¾ãã¦å¥ã®å¦çãè¡ãå ´åã¯ã©ããã¾ãã。ãã¨ãã°、List å
ã® String ã®é çªã(ã¬ã³ã¼ãã®ããã«)å³æ¹åã«å転ããã YouSpinMeRound ã¨ãã Continuation ãè¡ãå ´åã§ã。
å¥ã®ã¹ã¬ããã§å¦çãã¤ãã©ã¤ã³ã®å¥ã®ã³ã³ãã¼ãã³ããå®è¡ãããå ´åã¯ã©ããã¾ãã。
ç¾å®çã«、Task ã® Continuation ã¯、ãã¼ã¿ã»ããã«å¯¾ãã filter、map、reduce æ©è½ã®ä¸é£ã®ã¢ã¸ã¥ã¼ã« ãã§ã¼ã³ãå®è¡ããããã«ä½¿ããã¨ãå¤ãã§ããã。ã¾ã、ã³ã¬ã¯ã·ã§ã³ã大ãããªãå¯è½æ§ãããå ´åã¯、ãããã£ãåä½ãã¡ã¤ã³ã¹ã¬ãã以å¤ã§è¡ãããå ´åãããã¾ã。ããã、ããã§é³æ¥½ã楽ãããã¨ãã§ããã§ããã!
Task ã®ãã§ã¼ã³ã®å®è¡ä¸ã§ã¨ã©ã¼ãçºçããã
æå¾ã«、Continuation ã«ã¤ãã¦ãã 1 ã¤ç¥ã£ã¦ããã¹ããã¨ãããã¾ã。ã©ããã®ã¹ãã¼ã¸ã®å¦çä¸ã«ã©ã³ã¿ã¤ã ä¾å¤ãã¹ãã¼ãããå ´å、é常、ãã®ä¾å¤ã¯ãã§ã¼ã³ã®æå¾ã® Task ã®å¤±æãç£è¦ãããªã¹ãã¼ã¾ã§ä¼æããã¾ã。ä»»æã® Continuation ã§ã¯、å
¥å Task ã«å¯¾ã㦠isSuccessful() ã¡ã½ãããå®è¡ã、ã¿ã¹ã¯ãæåãããã©ããã確èªã§ãã¾ã。ã¾ãã¯、(ä¸è¨ã®ä¾ã§è¡ã£ã¦ããããã«)åã« getResult() ãå¼ã³åºãã¨、以åã«å¤±æãããã°ä¾å¤ãåã¹ãã¼ãã、èªåçã«æ¬¡ã® Continuation ã«ãã©ãçãã¾ã。ãã ã、失æããå¯è½æ§ãããå ´å、ãã§ã¼ã³ã®æå¾ã® Task ã®ãªã¹ãã¼ã§å¿
ã失æã確èªããããã«ãã¾ã。
ãã¨ãã°、ä¸è¨ã®ãã§ã¼ã³ã® CarlyCallable ã null ãè¿ããå ´å、SeparateWays ãè¡ã Continuation ã NullPointerException ãã¹ãã¼ã、ãããæå¾ã® Task ã¾ã§ä¼æãã¾ã。Task ã« OnFailureListener ãç»é²ããã¦ããå ´å、åãä¾å¤ã®ã¤ã³ã¹ã¿ã³ã¹ã«ãã£ã¦ãããå¼ã³åºããã¾ã。
ãããã¯ã¤ãº
å
ã»ã©ã®ãã§ã¼ã³ã§、å¦çã³ã³ãã¼ãã³ããå¤æ´ããã«å
ã®æååã«å«ã¾ããåèªã®æ°ãè¦ã¤ãåºã æãå¹çç ãªæ¹æ³ã¯ä½ã§ãããã。ç¶ããèªãåã«、å°ãèãã¦ã¿ã¦ãã ãã。
çãã¯、ããããçãããèãããããã·ã³ãã«ãªãã®ã§ã。並ã³é ãç¡ä½çºã«ãªã£ã¦ããã ããªã®ã§、æãããããããã½ãªã¥ã¼ã·ã§ã³ã¯、æçµçãªåºåæååã®åèªã®æ°ãæ°ãããã¨ã§ã。ããã、ããã«ãã 1 ã¤ã®ããªãã¯ãæ½ãã§ãã¾ã。continueWith() ãå¼ã°ãããã³ã«æ°ãã Task ã¤ã³ã¹ã¿ã³ã¹ãè¿ããã¾ãã、ãããã¯ããã§åç
§ãããã¨ãã§ãã¾ãã。ãªããªã、ãã§ã¼ã³æ§æãå©ç¨ãã¦æå¾ã® Task ã«é£çµããã¦ããããã§ã。ãã®ãã、ä»»æã®ã¿ã¹ã¯ãã¤ã³ã¿ã¼ã»ãããã¦、次㮠Continuation ã«å ãã¦、å¥ã®ãªã¹ãã¼ã追å ããã¨ããæ¹æ³ãã¨ãã¾ã。
Task<List<String>> split_task = Tasks.call(new CarlyCallable())
.continueWith(executor, new SeparateWays());
split_task =
.continueWith(executor, new AllShookUp())
.continueWith(executor, new ComeTogether());
split_task.addOnCompleteListener(executor, new OnCompleteListener<List<String>>() {
@Override
public void onComplete(@NonNull Task<List<String>> task) {
// Find the number of words just by checking the size of the List
int size = task.getResult().size();
}
});
playlist.addOnCompleteListener( /* as before... */ );
Task ãå®äºããã¨、 両æ¹ã® Continuation ãå®è¡ãã、ããã¨ã¨ãã« ãã¹ã¦ã® 追å ããããªã¹ãã¼ãå®è¡ããã¾ã。ããã§è¡ã£ãã®ã¯、SeparateWays ã¨ãã Continuation ã®åºåãåå¾ãã Task ãã¤ã³ã¿ã¼ã»ããã、ãã®åºåãç´æ¥ãªãã¹ã³ããå¦çã追å ããã ãã§ã。Continuation ã®ãã§ã¼ã³ã«ã¯å½±é¿ãã¾ãã。ã¤ã³ã¿ã¼ã»ããããã¿ã¹ã¯ã§ List ã«å¯¾ã㦠size() ãå¼ã³åºãã ãã§、åèªã®æ°ãåå¾ã§ãã¾ã。
ãã®ã·ãªã¼ãºã®ãã¼ã 3 ã®ã¾ã¨ã
ãããããªåè«ã¯ãã¦ãã、Task API ã使ç¨ããã¨、ã·ã¼ã±ã³ã·ã£ã«ãªãã¤ãã©ã¤ã³å¦çãç°¡åã«ã¢ã¸ã¥ã¼ã«å½¢å¼ã§è¡¨ç¾ãã¦å®è¡ãããã¨ãã§ãã¾ã。ããã«、å¦çã®åã¹ãã¼ã¸ã§ã©ã® Executor ã使ãããæå®ã§ãã¾ã。ãã®å¦çã¯、Firebase ãã¢ããªã«çµã¿è¾¼ããçµã¿è¾¼ã¾ãªããã«ããããã、ç¬èªã® Task ã Firebase API ã® Task ãå©ç¨ãã¦å®è¡ã§ãã¾ã。æ¬ã·ãªã¼ãºã®æçµåã¨ãªã次åã§ã¯、Task ã使ã£ã¦è¤æ°ã®ä½æ¥ãåæå®è¡ãã並åå¦çã®èµ·åæ¹æ³ã«ã¤ãã¦è¦ã¦ããã¾ã。
ãã¤ãã®ããã«、質åãããã¾ããã、Twitter ã§ããã·ã¥ã¿ã° #AskFirebase ãæ¤ç´¢ããã、firebase-talk Google Group ããå©ç¨ãã ãã。å°ç¨ã® Firebase Slack ãã£ãã«ãç¨æãã¦ãã¾ã。Twitter 㧠@CodingDoug ã®ãã©ãã¼ããããããé¡ããã¾ã。ã·ãªã¼ãºã®æ¬¡ã®è¨äºãæ稿ãããéã«ãç¥ãããããã¾ã。
æå¾ã«、ãã®æ稿ã«ç»å ´ãããã¹ã¦ã®æ² ãä¸ãã¦ããã¾ããã。
Posted by Khanh LeViet - Developer Relations Team