Create or update record in ActiveAndroid ORM - android

I'm using active android as orm in my android project.
after I get a json response from server, I wanna create a field if that item does not exist, or update it if that item already exists.
Record existence is determined via a field named slug.
How can I achieve this via ActiveAndroid? since I don't see how to achieve in the wiki.

Assume your class name is YOUR_CLASS which has a slug field. Then do the following with ActiveAndroid:
YOUR_CLASS item = new Select()
.from(YOUR_CLASS.class)
.where(slug = ?,_slug_value_)
.executeSingle();
if(item == null) {
item = new YOUR_CLASS();
item.slug = _slug_value_;
}
//change what you want then save the item.
item._field_to_change_ = _new_value_;
item.save();

Related

How to save a list of objects with Proto DataStore

If I have the following class, how can I save a list of it with Proto DataStore?
data class Tag(
val id: int,
val name: String
)
All guides that I saw were teaching how to save only a single object. Is it possible to have a list of it?
You should consider storing list of content in Room, Even proto-datastore isnt a proper solution to store complex stuff,
If you still want then, I will suggest you to restrict the data stored to 10-15 items
to the code --->
Create your proto file, repeated is used to create list type for Java
message Student {
string id = 1;
string name = 2;
}
message ClassRoom {
string teacher = 1;
repeated Student students = 2; // repeated => list
}
Inside your proto-store,
dataStore.updateData { store ->
store.toBuilder()
.clearStudents() // clear previous list
.setAllStudents(students)// add the new list
.build()
}
if you want example checkout my sample app, read the data/domain layer
https://github.com/ch8n/Jetpack-compose-thatsMine

How to make query for table with column contains Pointer value?

I have object called Reservation which contains column with name "object_entity" and inside is ParseObject from table Entity. I want to query only those Reservation which contains certain Entity.
val q = ParseQuery.getQuery<ParseObject>("Reservation")
if (!isOnline(c)){
q.ignoreACLs()
q.fromLocalDatastore()
}
q.whereEqualTo("object_entity", ticket.getParseObject("object_entity")?.objectId)
q.findInBackground { itemList, err ->
itemList //empty size 0
err //null
}
This is working as intended if you are online. But if you are offline it will return empty list. I've checked local datastore and all expected objects are there. But if I remove q.whereEqualTo it will return list of all Reservations without any problem (but I need specific ones).
ticket.getParseObject("object_entity")?.objectId is not null. It always contains objectId - checked in debugger
If I remove q.whereEqualTo I check all returned Reservation objects if they contains Entity.
This is inside findInBackground:
itemList.forEach { reservation ->
val entityObj = reservation.getParseObject("object_entity")
if (entityObj == null) createLog("FoundObj", "null") else
createLog("FoundObj", entityObj.objectId.toString())
}
Ive tried to add inside q.whereEqualTo specific Entity objectId. And it returned empty list. But if I removed q.whereEqualTo that specific object with specific objectId I wanted to get was returned in list.

Unable to fetch the list column of AWS DynamoDB in Android Kotlin

I am developing an Android application using Kotlin and AWS DynamoDB. I am new to both technologies. What I am doing now is I am trying to scan data from a table of DynamoDB. I know how to scan it. But the problem is that one of the column has List data type.
I have a table called item with the following columns.
Note in particular the Images field.
In Kotlin Android, I scan the table like this.
val dynamoDBClient = AmazonDynamoDBClient(AWSMobileClient.getInstance().credentialsProvider)
val fetchedItems: ArrayList<Any> = ArrayList();
val scanRequest = ScanRequest().withTableName(MainApplication.DB_TABLE_ITEMS);
scanRequest.exclusiveStartKey = lastEvaluatedKey
val scanResult = dynamoDBClient.scan(scanRequest)
scanResult.items.forEach { item ->
Log.i("ITEM_NAME", item.get("Name")?.s)
val viewItem = ItemDO()
viewItem.id = item.get("Id")?.s
viewItem.description = item.get("Description")?.s
viewItem.name = item.get("Name")?.s
viewItem.userId = item.get("UserId")?.s
viewItem.images = item.get("Images")?.ns
fetchedItems.add(viewItem)
Log.i("IMAGES_COUNT", item.get("Images")?.ns?.size.toString())
}
But this
item.get("Images")?.ns
always return null even if the data exists in the column as in the screenshot below.
Why my code is not fetching the list data type but others?
The code looks good and should be returning data for all the attributes irrespective of their type. I have equivalent piece of code in java that works as expected. Can you try inspecting the value returned by item.get("Images") before making the null-safe call. Type of the value returned by item.get("Images") is AttributeValue and so there is a possibility that the value gets lost in the course of implicit type conversion.

How to delete specific field using Objectify?

I'm trying to delete specific field from my DB table. But unfortunately unsuccessfully. I tried this code:
Query<Movie> query = ofy().load().type(Movie.class).filter("name =", "movie name");
QueryResultIterator<Movie> queryIterator = query.iterator();
while (queryIterator.hasNext()){
Movie m = queryIterator.next();
if(p.getYear()!= null){
ofy().delete().entity(p.getYear()).now();
}
}
I also tried this:
Movie m = ofy().load().type(Movie.class).id("movie id").now();
List<Long> actors = m.getActor();
ofy().delete().entity(actors).now();
But it also didn't work.
What did i miss?
You need to set the property to delete to null and then save the entity. A delete will delete the whole entity, not any single property.
Example:
Movie m = ofy().load().type(Movie.class).id("movie id").now();
m.actors = null;
ofy().save().entity(m).now();

pass an array objects in Corona with class

I'm creating an app with Corona structured in Class and I have a problem when I want pass an array objects for create an object.
I have this:
main.lua
local SurpriseBoxClass = require("SurpriseBox")
local BoxClass = require("Box")
local box1 = BoxClass.new('palo', 'images/chestClose.gif', 'OPEN')
local box2 = BoxClass.new('moneda', 'images/chestClose.gif', 'OPEN')
boxes = { box1, box2 }
local game = SurpriseBoxClass.new(boxes)
SurpriseBox.lua
local SurpriseBox = {}
local SurpriseBox_mt = { __index = SurpriseBox }
function SurpriseBox.new(boxesAux)
local object = {
boxes = boxesAux
}
return setmetatable( object, SurpriseBox_mt )
end
The problem is when I want to print the content of array in a method of SurpriseBox, and the program said me that the array is nil if for example I do this:
print(boxes[0])
What can I do?
Thanks!
Lua tables are 1-based.
Try print(boxes[1], boxes[2]).
It will print the table id. If you need to print the contents of the table, you must iterate over its fields, or use a custom printer that does it for you (see "Print a table recursively").
Look at the function SupriseBox.new(boxesAux) (where I gather you desire to do the printing):
In object, you are associating the key "boxes" with the table boxesAux. This to access the contents of boxesAux via object you must go through the following process:
object["boxes"] or object.boxes will get you to boxesAux, to go into that you need the superscripting i.e [1]
print(object["boxes"][1]) --etc..
print(object.boxes[1]) --etc..
Note that, this will now give you box1. If you want to print a meaningful display of it's content (that is if the class isn't overloaded) you should use a pretty printing library.

Categories

Resources