Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Support for List #33

Closed
AkshayChordiya opened this issue Jan 5, 2015 · 6 comments
Closed

Support for List #33

AkshayChordiya opened this issue Jan 5, 2015 · 6 comments

Comments

@AkshayChordiya
Copy link

Hi,

This library already looks great, but I have read all the docs you provided but I didn't read anywhere that this library supports any type of List like Array List etc.

Can you please let me know if it supports?

Thank you.

@nhachicha
Copy link
Owner

Hi @AkshayChordiya

You can either convert your list into an array

 ArrayList#toArray(T[])

then save it. (check BasicOperations#testObjectArray class)
or use getObject

snappyDB = DBFactory.open(getContext(), dbName);
        List<Book> books = new ArrayList<>(3);
        books.add(new Book("Echo Burning", "0-399-14726-8"));
        books.add(new Book("Nothing To Lose", "0-593-05702-3"));
        books.add(new Book("61 Hours", "978-0-593-05706-3"));

        snappyDB.put("books", books);

        ArrayList<Book> mySavedBooks = (ArrayList<Book>) snappyDB.getObject("books", ArrayList.class);

        assertNotNull(mySavedBooks);
        assertEquals(mySavedBooks.size(), books.size());
        assertEquals(books.get(0).hashCode(), mySavedBooks.get(0).hashCode());
        assertEquals(books.get(1).hashCode(), mySavedBooks.get(1).hashCode());
        assertEquals(books.get(2).hashCode(), mySavedBooks.get(2).hashCode());

cheers

@AkshayChordiya
Copy link
Author

@nhachicha
Thank you so much.

@AmirAzizi
Copy link

Hi,
can I use this library for nested object (foreign key)
How can I save them ?
Thank you.

@SophieKrt
Copy link

Hello, I want to save an ArrayList of objects into the SnappyDB and retrieve it, when necessary. I follow the instructions as given and the arrayList is saved, but when I try to retrieve it from the DB, the error caught is : "Maybe you tried to retrieve an array using this method ? please use getObjectArray instead Class cannot be created (missing no-arg constructor): com.google.gson.JsonPrimitive".
My code is :
public static ArrayList getOfflineResearches() {
ArrayList mySavedResearches = null;
try {
mySavedResearches = (ArrayList) snappy.getObject(Constants.RESEARCHES_OFFLINE, ArrayList.class);

    } catch (SnappydbException e) {
        e.printStackTrace();
    }
    if (mySavedResearches != null) return mySavedResearches;
    return new ArrayList<>();
}

Any ideas?

@davidtcdeveloper
Copy link

@SophieKrt
How to you save the data? Instead of saving the ArrayList, try to convert the list into an array of objects and then convert it back when restoring.
It works this way here.

@SophieKrt
Copy link

Thank you , I will try to save my objects this way.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants