How to deal with nested Realm transactions in Xamarin? - xamarin

I've been trying to make sense the following article explaining how to use Realm in Xamarin:
https://blog.xamarin.com/cross-platform-development-with-xamarin-forms-and-realm/.
Realm seems to integrate nicely with the xaml UI for simple objects, but how does this translate to more complex objects that have children?
When showing the add/edit page for an object, I need to show a list with all the object's children. I want to be able to show the add/edit page for those children when tapped in the list. The problem is that there is already a transaction open in the first add/edit page and I can't open a second one.
The only option I see so far is to not use transactions and to bind fields to separate member variables then copy those values in a Realm object and save it. But then I don't see the real advantage in using Realm.
Thank you!

Related

How to add to an ObservableCollection from a different view?

(I'm using Prism Dryloc) My application consists of two views. The first contains a single list view displaying strings and the second - an entry and a button.
The listview is bound to an observable collection in the first page's View Model. How can I add to the observable collection from a different view?
Great question! You're essentially trying to pass data between views, and there's several ways of passing data between Views in Xamarin.Forms.
The two ways that seem relevant for your case:
Either making the ObservableCollection a public static object (so there's only one global instance of it). Not recommended.
The better way is to use the messaging center so that the second page publishes an event when the button is pressed, that the first page is subscribed to. And it passes that information that gets added to the list.
If these don't work, elaborate your use case and I'll suggest some more

CRM 2013/2015/2016 subgrid popout, retain fetchxml query

I have a subgrid on my form which I assign a dynamically generated fetchxml query using javascript. When the user clicks on the subgrid's "pop out" button, the query is lost and it displays the default view for that entity.
The user needs to be able to perform multi selection and bulk editing. As far as I know this is not possible for subgrids in CRM 2013 upwards. Is there a way to retain the query when it is popped out?
There's no supported way to interact with a popped out grid. If you started digging into the client side application code, you might be able to find a hook to the new window, but doing so would be unsupported, liable to break, and I wouldn't recommend it.
I was going to suggest hiding that pop-out button on your entity's sub-grid altogether before re-reading that your users need to use the popped out grid for bulk edit.
In that case your best option will be to do ditch the JS and move to a RetrieveMultiple plugin against the sub-entity. RetrieveMultiple plugins should be generally avoided, but from what it sounds like in your case, it's your only option.
And, just to be thorough, are you positive you need dynamic fetch for the sub-grid? Is there any way you could come up with a view that would get close enough to your requirement, even if it's not perfect?
Update:
Based on the additional information you provided in your comment, I can only think of two options:
Implement a custom grid--or modify one of the free ones out there--that allows multi-select and bulk edit
Your custom web app takes the user's criteria, creates a personal view (entity: userquery) for the user and saves it to the database, then your JS on the form sets the grid to that view.
You can do that liks this:
document.getElementById("someGridId").control.SetParameter("viewtype", "4230"); // 4230 is the objecttypecode for userquery
document.getElementById("someGridId").control.SetParameter("viewid", "{11310965-0306-E611-80E5-3863BB36DD08}");
Doing this actually does make the popped out grid load the personal view, but in my tests just now the grid gets upset if you try to refresh it.
This approach has obvious downsides: 1) You have to come up with a way to clean up the personal views that get generated 2) the grid might break on refresh 3) it's unsupported and liable to break on an update.
Still worth considering, I suppose.
A more supported way instead of using unsupported getElementById is to hook a plugin to multiple retrieve of the entity and amend the query in there
Chris

Setting DataContext in UserControl from ViewModel

I have a View that contains a Listbox. I'll call it CityListingPage.xaml. This list page has a CityListingViewModel, that is binded with the View like somewhat like this:
DataContext="{Binding CityListing, Source={StaticResource Locator}}"
This works nicely. Now I what to change my page to a Pivot Control, where the Pivot Items, would be instances of CityListingViewModel, but obviously with different constructor data (ie. Country)
I extracted the ListBox into a UserControl. Now I'm struggling how to make this work so that for each list I get a new instance of the CityListingViewModel.
I tried creating in the ViewModelLocator a collection of CityListingViewModels but how do I pass the a CityListingViewModel instance to the UserControls DataContext?
Perhaps there is a different, better way of doing this?
Without seeing your code, I'm going to do a little guessing, but I think you can do it directly via data binding. Since each pivot item is getting an instance of CityListingViewModel, you can just pass that binding along to the UserControl:
If you post a little more code showing what you're trying to do, we might be able to be of more help.
The following are two answers for using a collection to create panorama pages. But I am quite sure that the approach can be adapted to pivot pages:
Dynamically add eventtocommand actions to a listbox
Static and dynamic panorama items in a panorama wp7 mvvm
The second post should be more relevant.
If you are thinking of partitioning the same data over multiple views on a pivot page then I would suggest NOT using several view models, especially if it is the same datasource you are using for all the data.
Simply have a parameter which each view would bind to and use Linq to control what data is visible to that parameter.
So you will have the variable which will contain all the data to be displayed and one parameter per view querying that data.

NSArrayControllers and heterogeneous arrays of Core Data objects

I'm trying to create a Mac OS Core Data application that has an array of parent objects (called Levels) each of which contains a collection of child objects (called Blocks) via a one-to-many relationship. I have a table view successfully controlling the array of levels, and a custom view object that draws the blocks graphically based on positions held in x and y properties of my Block model class. I can add blocks to the currently selected level, remove them, select and move them around in the custom view, and have bound text fields to various other properties of the Block class which I can use to edit those values. All of this information is successfully saved and restored to and from the core data repository with no issues output to the debugger. Wonderful. I've used an NSArrayController for the Levels and another for the Blocks that is bound to the current selection of the Levels array controller, in what I've read is a pretty standard way.
Now, my Block class is actually an abstract class, and what I actually instantiate are various child classes of Block (eg RedBlock, GreenBlock, BlueBlock classes). Each sub-class has a separate set of properties that only apply to that type of block (so RedBlock has a "text" property that none of the others have, BlueBlock has an integer "value" property, etc). I want to create an inspector that will change depending on the type of the Blocks that are currently selected in my custom view. To try this, before I start creating subviews for each type of Bock, I have created a text field that I want to bind to the currently selected RedBlock's "text" property, preferably showing nothing when Blocks of other kinds are selected. This is where I'm stuck. I've added another NSArrayController in Entity mode with RedBlock specified as it's type so I can bind to the "text" property, and tried adding a filter predicate based on the class type. I've also tried various other configurations and bindings, but I'm either getting exceptions, or corrupt values in the text field that I bind to that controller, or other weird bugs and general brokenness.
I've googled around for an example of an inspector that can cope with a heterogenous array of objects (as that's essentially what I'm trying to do) but so far no luck.
So, my question is - am I going about this the right way? Should I be trying to create an NSArrayController that filters the selected items in my Blocks array controller somehow? If so, should that be straightforward or is there some trick that I've missed? If not, what is the best way to do this?
This approach should work, provided you limit the inspector to displaying view which bind to properties which apply to the entire selection. You don't need a second array controller.
To test the simple example, try creating a data set with only blocks, see that your bound control loads without raising exceptions, and that it updates the object correctly.
Once that is working, create separate views for each type, and display and hide them when the selection will change. Again, if you have a heterogenous selection, hide them all.

Populating a subform with different displays as a GUI in Access 2007

This is my first time building a UI in Access (using Access 2007), and I'm wondering what is the Right Way (TM) of going about this.
Essentially, I have several different queries that I'd like to display as pivot charts, pivot tables, tables, and reports. Eventually I'm also going to have to build forms to manipulate the data as well, but the application's primary function is to display data.
I'm thinking of having a button for each different display down the left side of the main window, and having the rest of the window display each button's corresponding contents (e.g. a pivot chart).
I have an idea that this can be accomplished using a single subform in the main form, and setting the subform's Source Object property within a function such as this one:
Public Function SetSubformSourceObject(newSourceObject) As Variant
subform.SourceObject = newSourceObject
End Function
Then, for each button I'd set its OnClick property to call this function with the name of the query I'd like to run.
Now, I have no idea if this is the best way of going about things, and would really appreciate some input :)
The principle seems fair to me. You have to give it a try. You do not even need a form-subform structure. You can set your sourceObject at the form level, and have your buttons in a commandBar instead of having them as controls on the form, so you do not have any 'form specific' code (like "onCLick") and controls. action/command controls on a form are space, code and maintenance consuming, while commandbars are more generic and are THE object that can hold all your action controls.

Resources