Parse.initialize()是一种即发即弃的异步方法吗?
请考虑以下代码:
申请类:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Initialize Crash Reporting.
ParseCrashReporting.enable(this);
// Enable Local Datastore.
Parse.enableLocalDatastore(this);
// Add your initialization code here
Parse.initialize(this);
ParseACL defaultACL = new ParseACL();
// Optionally enable public read access.
// defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
}
}
测试用例:
import android.test.AndroidTestCase;
import com.parse.DeleteCallback;
import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.parse.SignUpCallback;
import java.util.concurrent.CountDownLatch;
/**
* Created by alex on 31/03/15.
*/
public class UserTest extends AndroidTestCase {
public UserTest() {
super();
}
@Override
protected void setUp() {
}
interface SignupAndLoginCallback {
public void done(ParseUser user, ParseException e);
}
public void signupAndLogin(final SignupAndLoginCallback callback) {
ParseUser user = new ParseUser();
user.setUsername("olatest2");
user.setPassword("kari4ever");
user.setEmail("olatest2@nordmann.no");
user.put("phone", "650-253-0000");
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
ParseUser.logInInBackground("olatest2", "kari4ever", new LogInCallback() {
public void done(ParseUser user, ParseException e) {
callback.done(user, e);
}
});
}
});
}
public void testSignupLoginDestroy() throws InterruptedException {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ParseUser user = new ParseUser();
user.setUsername("olatest");
user.setPassword("kari4ever");
user.setEmail("olatest@nordmann.no");
user.put("phone", "650-253-0000");
final CountDownLatch latch = new CountDownLatch(1);
class Spy {
ParseUser user;
ParseException signupException;
ParseException loginException;
ParseException deleteException;
}
final Spy spy = new Spy();
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
spy.signupException = e;
ParseUser.logInInBackground("olatest", "kari4ever", new LogInCallback() {
public void done(ParseUser user, ParseException e) {
spy.loginException = e;
spy.user = user;
if (user == null || e != null) {
latch.countDown();
} else {
user.deleteEventually(new DeleteCallback() {
@Override
public void done(ParseException e) {
spy.deleteException = e;
latch.countDown();
}
});
}
}
});
}
});
latch.await();
assertNull(spy.signupException);
assertNull(spy.loginException);
assertNull(spy.deleteException);
assertNotNull(spy.user);
}
public void testCreateObject() throws InterruptedException {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
final CountDownLatch latch = new CountDownLatch(1);
class Spy {
ParseException loginException;
ParseException deleteException;
ParseException saveException;
ParseUser user;
}
final Spy spy = new Spy();
signupAndLogin(new SignupAndLoginCallback() {
@Override
public void done(ParseUser user, ParseException e) {
spy.loginException = e;
spy.user = user;
if (e == null && user != null) {
ParseObject gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);
gameScore.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
spy.saveException = e;
latch.countDown();
}
});
} else {
latch.countDown();
}
}
});
latch.await();
assertNull(spy.loginException);
assertNull(spy.deleteException);
assertNull(spy.saveException);
assertNotNull(spy.user);
}
}
如果从测试用例中删除Thread.sleep(1000);
次调用,有时测试会爆炸,但有例外:
java.lang.IllegalArgumentException: You must register this ParseObject subclass before instantiating it.
at com.parse.ParseObject.<init>(ParseObject.java:166)
at com.parse.ParseObject.<init>(ParseObject.java:127)
at com.parse.ParseUser.<init>(ParseUser.java:89)
at agens.no.gjensidigeparseandroidclient.UserTest.signupAndLogin(UserTest.java:35)
at agens.no.gjensidigeparseandroidclient.UserTest.testCreateObject(UserTest.java:122)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
我怀疑这是因为ParseUser
的寄存器代码尚未运行。
如果是这样,Parse可以向Parse.initialize()
添加回调吗?
答案 0 :(得分:-1)
这不是异步方法。你应该把钥匙添加到Parse.initialize()!教程示例具有误导性!
Parse.initialize(this,&#34; id&#34;,&#34; key&#34;);
https://www.parse.com/apps/quickstart#parse_data/mobile/android/native/new