简体   繁体   English

解析Android SDK,saveAll子类对象?

[英]Parse Android SDK, saveAll subclass objects?

I'm using the Parse SDK on Android. 我正在Android上使用Parse SDK。 There doesn't seem to be any documentation on using the ParseObject.saveAll(List<ParseObject>) method. 关于使用ParseObject.saveAll(List<ParseObject>)方法,似乎没有任何文档。 I have a subclass called Test which extends ParseObject . 我有一个称为Test的子类,该子类扩展了ParseObject I've been able to use most ParseObject methods so far, but I would like to save the whole list using the saveAll method, but it requires a List<ParseObject> and won't accept List<Test> . 到目前为止,我已经可以使用大多数ParseObject方法,但是我想使用saveAll方法保存整个列表,但是它需要一个List<ParseObject>并且不接受List<Test> Any ideas? 有任何想法吗?

Your ParseObject model may like this 您的ParseObject模型可能会这样

class Person extends ParseObject
{
    public static String NAME = “name”;
    public static int AGE = “age”;
    public void setName(String name)
    {
        put(NAME,name);
    }
    public void setAge(int age)
    {
        put(AGE,age);
    }
    public Stirng getName()
    {
        return get(NAME);
    }
    public int getAge()
    {
        return get(AGE);
    }
}

now,Person is subclass of ParseObject. 现在,Person是ParseObject的子类。 You can use ParseObject.saveAll(List personList) 您可以使用ParseObject.saveAll(List personList)

Person person1 = new Person();
person1.setName("Mike");
person1.setAge(18);
Person person2 = new Person();
person2.setName("John");
person2.setAge(25);
List<Person> personList = new ArrayList<Person>();
personList.add(person1);
personList.add(person2);
ParseObject.saveAll(personList);

Found a solution here: https://parse.com/questions/how-to-use-parseobjectsaveall-on-subclass 在此处找到解决方案: https : //parse.com/questions/how-to-use-parseobjectsaveall-on-subclass

Let's say you have your 3 Game object, game1, game2, game3, this should work: 假设您有3个游戏对象game1,game2,game3,这应该可以工作:

List mygames = new ArrayList(); 列出mygames = new ArrayList(); mygames.addAll(Arrays.asList(game1, game2, game3)); mygames.addAll(Arrays.asList(game1,game2,game3)); ParseObject.saveAllInBackground(mygames); ParseObject.saveAllInBackground(mygames);

An alternative solution could be to write a custom save all method inside the subclass: 另一种解决方案是在子类中编写一个自定义的save all方法:

public static void saveAllInBackground(
        final List<MySubclass> objects,
        final SaveCallback savecallback) {

    counter = 0;

    for (MySubclass mySubclass : objects) {
        mySubclass.saveEventually(new SaveCallback() {

            @Override
            public void done(ParseException e) {
                if (e == null) {
                    counter++;
                    if (counter == objects.size()) {
                        savecallback.done(null);
                    }
                } else {
                    savecallback.done(e);
                }
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM