Realm asObservable() emit only new items - realm

I'm new to RxJava, and trying to use the Realm Observable feature.
When doing this
realm.where(Intake.class)
.findAllSorted("time", Sort.DESCENDING)
.asObservable()
I get the full list of items, but when soemthing changes (ie item added), I get the full list again.
What is the RxJava-way to get only the new items?
Thanks in advance

You are currently observing the query results. Because your query can contain multiple items (findAllSorted()), you're always observing the RealmResults that will emit all sorted items when there's a change (see docs).
You can do something like this:
realm.where(Intake.class)
.findAllSorted("time", Sort.DESCENDING)
.asObservable()
.flatMapIterable(results -> results)
.distinct();
This does 2 more things:
convert your RealmResults to singular Intake instances
only let distinct items pass through (make sure your Intake implements equals() correctly)
This does however impose some extra CPU load, because each time the query passes on a new RealmResult, processing is done to filter out the distinct items.
In the above example, sorting will work on the initial set of emitted Intake objects. However, any subsequent emitted items could be observed out of order because they are new and emitted after the initial results.

Related

Is there a way to show the count of a nested collection in a CollectionEditor before it is expanded?

If a property in a CollectionEditor is itself an expandable collection, the "value" displayed for it is the string "(Collection)". The cascaded dialog that appears when expanding that collection may contain a lengthy list that takes several seconds, or even minutes, to display. A user who was aware of the collection count might choose to avoid expanding it, or at least would have an expectation of waiting.
I would like to replace or augment the displayed value with the count (size) of the collection. For example, instead of "(Collection)", I would like to see something like "(Collection: 50000)".

How do I create a random item spawner with no repeats?

I am currently working on a rogue-like game in GameMaker Studio 2 and i would like to have an item spawner where no items are repeated.
I have tried multiple different ideas of what i think would work, such as giving items and id variable and only spawning items which hasn't had it's id called, although it doesn't seem to work.
The code I have right now is basic, but that is because it's the only way I've been able to spawn an item, there is repeating items with what i have and i would like to stop that from happening.
Here is the Create code of the object:
// Items
var items = choose(
obj_homing,
obj_tracking,
obj_bounce,
obj_double_xp,
obj_shotgun,
obj_orbit,
obj_firefaster,
obj_scattershot,
obj_damageboost,
obj_explosive
);
instance_create_layer(x, y, "Items", choose(items));
I haven't had any actually crashes in the game, although the errors i have faced are multiple of the same object spawning twice when i would like items to not repeat.
One option is to modify the items array after each call to remove the spawned item, and re-initialize it as full only at the start of each game or when the array is empty.
The problem I'm seeing in looking through the Game Maker Language Documentation is that I cannot find a way to delete an object from an existing array and resize the array.
You're placing choose on both defining the var, and when creating the layer.
Now, I don't know exactly what choose does, but I assume it's choosing randomly a selection from the array.
So Items would only return a single chosen item from the array, maybe it's better to remove the choose function from the Items, and only decide that when creating the object.
So the Items should become an array:
var items = [
obj_homing,
obj_tracking,
obj_bounce,
obj_double_xp,
obj_shotgun,
obj_orbit,
obj_firefaster,
obj_scattershot,
obj_damageboost,
obj_explosive
];
Just save the random instance in a variable, then when you create a random instance, check if equals to the instance that you have created previously, and if it is, call again the function until the previous instance is different from the new generated instance.
If you want to spawn every item only once, how about changing the current spawned object in the array to "noone".
And at every spawn cycle, you would check, if the selected item is noone and if so, select the next item (array[i++]).
Also do not forget to randomize the seed, somewhere in the beginning of your game with randomize().

modify field value in a crossfilter after insertion

I need to modify a field value for all records in a crossfilter before inserting new records.
The API doesn't say anything about it. Is there a way to do that ?
Even if it's a hack that would be really useful to me.
Looking at the code, the data array is held as a private local variable inside the crossfilter function so there's no way to get at it directly.
With that said, it looks like Crossfilter really tries to minimize the number of copies of the data it makes. So callback functions like the ones passed into crossfilter.dimension or dimension.filter are passed the actual records themselves from the data array (using the native Array.map) so any changes to make to the records will be made to the main records.
With that said, you obviously need to be very careful that you're not changing anything that is relied on by the existing dimensions, filters or groups. Otherwise you'll end up with data that doesn't agree with the internal Crossfilter structures and chaos will ensue.
The cool thing about .remove is it only removes entries that match the currently applied filters. So if you create a 'unique dimension' that returns a unique value for every entry in your dataset (like an ID column), you can have a function like this:
function editEntry(id, changes) {
uniqueDimension.filter(id); // filter to the item you want to change
var selectedEntry = uniqueDimension.top(1)[0]; // get the item
_.extend(selectedEntry, changes); // apply changes to it
ndx.remove(); // remove all items that pass the current filter (which will just be the item we are changing
ndx.add([selectedEntry]); // re-add the item
uniqueDimension.filter(null); // clear the filter
dc.redrawAll(); // redraw the UI
}
At the least you could do a cf.remove() then readd the adjusted data with cf.add().

Qt error "persistent model indexes corrupted" why?

I've a problem with my Qt/interview application. I use QTreeView to display tree data. I implemented my own model based on QAbstractItemModel.
I get a following error prior to application crash. It happens often after I add new record.
Could You explain to me what is the meaning of this error. What is a QPersistentModelIndex ?
I'm not using QPersistentModelIndex in my code.
ASSERT failure in QPersistentModelIndex::~QPersistentModelIndex: "persistent model indexes corrupted"
Thanks.
QPersistentModelIndexes are (row, column, parent) references to items that are automatically updated when the referenced items are moved inside the model, unlike regular QModelIndex. For instance, if you insert one row, all existing persistent indexes positioned below the insertion point will have their row property incremented by one.
You may not use them directly, but QTreeView does, to keep track of expanded items and selected items, for example.
And for these persistent indexes to be updated, you have to call the functions QAbstractitemModel::beginInsertRows() and endInsertRows() around the actual row insertion(s) when you add new records.
See the end of the section about subclassing model classes for details: http://doc.trolltech.com/latest/qabstractitemmodel.html#subclassing
I found this method QAbstractItemModel::persistentIndexList and I'm
wondering what indexes it should return. All of them ?
Should this method return all nodes currently visible in the TreeView ?
That method returns only the indexes for which a QPersistentIndexModel was created and is still in scope (as a local variable, a class member, or in a QList<QPersistentIndexModel> for example).
Expanded or selected nodes are not necessarily currently visible, so you can't (and shouldn't anyway) assume anything about what these persistent indexes are used for.
You just have to keep them updated, and you only need to use persistentIndexList for big changes in the model, like sorting (see QTreeWidget internal model : QTreeModel::ensureSorted(link)), for smaller incremental changes you have all the beginXxxRows/beginXxxColumns and endXxxRows/endXxxColumns methods.

How to retrieve the sum of items from a sharepoint list

Is there any way to get the sum of items based on some filters from sharepoint list? I was trying to access the GetListItems method. But this returns all the items in the list. That makes the data heavy. My requirement is to get only the sum of items.
For example items created in a specific year. I am trying to populate a chart in flex from the sharepoint list. Accessing all the items and then calculating the sum in flex will not work always where the list contains more items.
Use the viewFields parameter of GetListItems to only return a single (small) field like ID. It is still data heavy but better than returning all the fields from the list.
There is a rowLimit parameter too that you can use to return all the list items instead of only a block at a time.
Info on the GetListItems method with code snippets at MS:
http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems(v=office.12).aspx

Resources