How to initialize a Ref<?> field in objectify to a dummy value - google-app-engine

I have a collection(arraylist) of Ref `s ,the objectify documentation says that I need to initialize collections for them to be persisted and hence modified in the future.....
Now , Ref points to an object but when I launch my app for the first time I dont have any objects in the data store...so whats the best way for me to initialize a dummy value......
Is my assumption that a Ref<> needs to point to a real object in the data store?

Two things:
You should just initialize an empty collection. You don't need to add anything to it. eg, field = new ArrayList<Ref<Thing>>();
It's actually not even required that you initialize the collection. It's just a good idea for reasons that will become apparent if you use the system for a while.

Related

angular one way binding

I am using angular 1.3.15. I want to bind data, such a way that, first time variable($scope.twotap_builtin_cart.sites[sikey].shipping ) assigned data from $scope.shipping_address. Later on even if , variable named $scope.twotap_builtin_cart.sites[sikey].shipping data modified, it should not haveve any impact on other $scope.shipping_address. I am talking about one time binding or one way binding
you should use angular.copy() for deep coping
$scope.shipping_address = angular.copy($scope.twotap_builtin_cart.sites[sikey].shipping)
this way even if $scope.twotap_builtin_cart.sites[sikey].shipping modify its not gonna bind to the $scope.shipping_address
I think that you are not looking for binding but simply assigning a value of a variable to another. When working with JSON objects (and $scope is one such object), making a = b is NOT copying the contents of b to a, but making both a and b reference the same object. The best way is to perform the assignment as:
b = JSON.parse(JSON.stringify(a)) ;
In your case:
$scope.twotap_builtin_cart.sites[sikey].shipping = JSON.parse(JSON.stringify($scope.shipping_address)) ;
One you do this, both variables hold the same information but they can be changed without affecting the other.
A similar requirement was asked in this question:
Edit with bootstrap modal and angulajs
Similarly, you can use the AngularJS copy function to replicate your data without any lingering bindings.
$scope.twotap_builtin_cart.sites[sikey].shipping = angular.copy($scope.shipping_address);
Here we are copying the value from $scope.shipping_address into the other variable. Now even if you make a change to $scope.twotap_builtin_cart.sites[sikey].shipping, this will not be reflected in $scope.shipping_address - which is what you want.

Is it bad programming practice to store objects of type Foo into a static array of type Foo belonging to Foo in their construction?

Say I wanted to store objects statically inside their own class. Like this:
public class Foo
{
private static int instance_id = 0;
public static List<Foo> instances = new List<Foo>();
public Foo()
{
instances[instance_id++] = this;
}
}
Why?
I don't need to create unique array structures outside the class (one will do).
I want to map each object to a unique id according to their time of birth.
I will only have one thread with the class in use. Foo will only exist as one set in the program.
I did searching, but could find no mention of this data structure. Is this bad practice? If so, why? Thank you.
{please note, this question is not specific to any language}
There are a couple of potential problems I can see with this setup.
First, since you only have a single array of objects, if you need to update the code so that you have lots of different groups of objects in different contexts, you'll need to do a significant rewrite so that each object ends up getting associated with a different context. Depending on your setup this may not be a problem, but I suspect that in the long term this decision may come back to haunt you.
Second, this approach assumes that you never need to dispose of any objects. Imagine that you want to update your code so that you do a number of different simulations and aggregate the results. If you do this, then you'll end up having your giant array storing pointers to objects you're not using. This means that you'll (1) have a memory leak and (2) have to update all your looping code to skip over objects you no longer care about.
Third, this approach makes it the responsibility of the class, rather than the client, to keep track of all the instances. In some sense, if the purpose of what you're doing is to make it easier for clients to have access to a global list of all the objects that exist, you may want to consider just putting a different list somewhere else that's globally accessible so that the objects themselves aren't the ones responsible for keeping track of themselves.
I would recommend using one of a number of alternate approaches:
Just have the client do this. If the client needs to keep track of all the instances, just have them always create the array they need and populate it. That way, if multiple clients need different arrays, they can do so. You also avoid the memory leak issues if you do this properly.
Have each object take, as part of its constructor, a context in which to be constructed. For example, if all of these objects are nodes in a quadtree, have them take a pointer to the quadtree in which they'll live as a constructor parameter, then have the quadtree object store the list of the nodes in it. After all, it seems like it's really the quadtree's responsibility to keep track of everything.
Keep doing what you're doing, but using something with weak references. For example, you might consider using some variation on a WeakHashMap so that you do store everything, but if the objects are no longer needed, you at least don't have a memory leak.

Storing the value with the Ref, as long as it's not in the datastore

I'm have a List<Ref<Entity>>. I add new entries to the list like this:
entities.add(Ref.create(new_entry));
modified.add(new_entry);
When I store the entity that contains the list, I store the list itself and all the entities that are in the modified list. This works fine.
The problem is, that I have to work with the entities-list, while I add new entries to it. This requires iterating the list multiple times. The problem here is, that the refs in the list point to old entries (which are already in the datastore) and new entries (which are not yet in the datastore).
This causes the Ref.get()-method to return null for all the yet unstored entries in the list (the ones that are still in the modified-list).
I worked around this by doing this when inserting:
Ref<T> ref = new DeadRef<>(
Key.create(data),
data
);
this.entities.add(ref);
this.modified.add(data);
This way, I can mix stored and unstored entries in one list and Ref.get() always returns a value.
This works, but I have noticed that the refs in the entities-list stay DeadRefs when I store them to the datastore and load them in again.
Will this be a problem? Is there maybe even a better way to accomplish this?
This seems like a bad idea, although I don't know what specific problems you will run into.
The "right answer" is to save your entities first.
Edit: Also look at the documentation for ofy().defer().save(), which can prevent you from issuing a lot of unnecessary save operations.

Is there a way to ensure that a single reference to an object exists at a point in time?

I am not sure the practical value of such a thing, but I am wondering if in for example Java, an object can be instantiated so that if a variable holds a reference to it, no other variable can do so unless the first variable no longer does. The object could only be in a single list. Intuitively, this would correspond more to real life objects that can only be in one place at a time.

Parse.com database logic for pointers

Just for my info because I tried to figure out last time even asked their support but unfortunately still no answer.
It's a simple problem I think but I can't wrap my head around.
Let's say I have a object A,object B, object c.
I query to object A that has a pointer to object B. I get all the data back with include key and it works nice. But object B has also a pointer to object C. That data is empty. How can I get that pointer data too in the same query ?
Assuming these columns:
ObjectA:
pointerToB : Pointer<ObjectB>
ObjectB:
pointerToC : Pointer<ObjectC>
You use dot-syntax, in JavaScript:
objectAQuery.include("pointerToB.pointerToC");
You have to use the column names, the above will cause both to be populated.

Resources