简体   繁体   中英

Parse Android SDK, saveAll subclass objects?

I'm using the Parse SDK on Android. There doesn't seem to be any documentation on using the ParseObject.saveAll(List<ParseObject>) method. I have a subclass called Test which extends 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> . Any ideas?

Your ParseObject model may like this

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. You can use 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

Let's say you have your 3 Game object, game1, game2, game3, this should work:

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

An alternative solution could be to write a custom save all method inside the subclass:

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);
                }
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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