Coredata attribute join - core-data

Hi I have a Core data entity that has a few attributes like displayName, serverName and portNumber.
I want the serverName and portNumber to be user editable. I want the display name to be auto generated using the serverName and portNumber values - something like "Server # Port"
What is the best way of implementing the displayName attribute ? Is there anything like trigger in Core Data that would auto populate this attribute ?

You can simply add a method to your managed object subclass without the need to save this information again the database.
-(NSString*)displayName {
return [NSString stringWithFormat:#"%# : %#", self.serverName, self.portNumber];
}
Tipp: use a separate file with a category on your subclass. In this way, in case the object model changes, you can use Xcode to generate the subclass again without overwriting your custom code.

Related

Cocoa Bindings not updating NSTextField value

I have two windows, one is a table view to display content. One is a window for inputting content.
I have an NSTextField binded to a property in my app delegate. When I change a value in the text field, the app delegate property will change. However, if I go the other way and programmatically change the property's binded key, the value of the text field does not update. However, it does in the table view.
What is going wrong here? How can I update text field?
EDIT:
I tried all 3 of these with the same result
[_addEntry setValue:#"Chet" forKey:#"payee"];
[_addEntry setPayee:#"chet"];
_addEntry.payee = #"chet";
EDIT:
Here's a simple example to elaborate on my point
https://dl.dropboxusercontent.com/u/48014761/test.zip
the label and the textfield are bound to the "str" property. It is initially null. press log to see that in the console. press change str button to change the string. Log to verify. Note that the label and the textfield do not update!
EDIT:
"What am I trying to accomplish?"
Here's the project I am working with:
https://dl.dropboxusercontent.com/u/48014761/Write-Offs.zip
I am trying to use Cocoa Bindings as much as possible. I have a table with a bunch of data. However, each entry is going to have an array of images associated with it. Thus, when I add a new entry, I need to open up a new window so I can set the properties of that entry (rather than through the table) along with upload some images.
I am not sure I am implementing the addEntryWindow correctly. It seems like I should create and destroy the window each time I open and close it. This doesn't appear to be happening.
Also, [[self addEntry] setDate:[NSDate date]]; doesn't seem to fix the problem for me.
Thanks
Chet
How are you programmatically changing the field? Are you using KVO? Is the field inside of a NSManagedObject instance?
Bindings work through KVO. Core Data disables some aspects of KVO for its own internal uses and you might be tripping over one of those.
Update your question with the code sample and lets see what is going on.
Update
Both windows don't necessarily need to be in the same xib but they do need to be talking to the same instances. Ideally they should both be talking to the same instance of NSManagedObject and therefore talking to the same NSManagedObjectContext.
Who is the owner of each window?
Is the owner the same?
If not, is a new Core Data stack being created?
Update
In your test, I was able to correct it by how you were editing the property
- (IBAction)press:(id)sender {
[self setStr:#"this"];
}
You were accessing the iVar directly instead of the property. When you access the iVar directly KVO does not fire.
In addition, in your xib files you were accessing self.str which is unnecessary. It should be just str.
Are you doing any direct property access in your actual project?
Update
You can do a get accessor and then a set accessor:
[[self addEntry] setPayee:xxx];
Based on the variables you are using I wonder what you are trying to accomplish. Can you post the exact code of the programmatic change you are trying to enact?

CRM Restkit oData call to retrieve custom entity's value in crm 2011

In crm 2011, in an entity I have to retrieve the first name field from a custom entity through CRM Restkit.
I am getting an error when I run the code.
I think the 'filter' is wrong.
If it is system entity, then filter = "ContactId eq guid'"+Xrm.Page.data.entity.attributes.get('xyz' ).getValue( )[0].id+"'";
works fine.
But my case it is a custom entity with schema name 'new_student',
I tried the filter = "new_student/Id eq guid'"+Xrm.Page.data.entity.attributes.get('xyz' ).getValue( )[0].id+"'";
which is not working.
So what should the filter be in my case
Regards,
Vickram
You probably should not use the entity name as the prefix in the filter, 'new_student/Id'. But that being said, I would suggest you "debug" this problem in few steps.
Start by opening the "new_student" JSON feed URL at the JSON endpoint to see that everything is valid for your custom entity through the web services, like:
http://yourCRMServer/YourInstanceName/2011/OrganizationData.svc/new_studentSet
Just replace the CRM server URL and instance name.
When you get this path working and you see "results", continue adding the filter to the URL helping you to identify the field name and finding the correct filter faster:
http://yourCRMServer/YourInstanceName/2011/OrganizationData.svc/new_studentSet?$filter=Your_Schema_Field_Name eq 'Johnson'
and so on, then if you only need the first name you should add that to the $select to minimize the load of data coming from the JSON web-service:
http://yourCRMServer/YourInstanceName/2011/OrganizationData.svc/new_studentSet?$filter=Schema_Field_Name eq 'Johnson'&$select=FirstName
Hope it helps.

CoreData: Save Modified Managed Object as New Object

My app allows a user to edit data, but during save there are two things that can happen:
If the name of the data stayed the same, just save the object as an edited version. I.e. they are just editing the existing object.
However, if they have changed the name, this should create a new instance and restore the edited data to the original.
Obviously, 1 is the easy case and is working just fine. But I'm conflicted about the best method to handle 2. How best is it to save a modified NSManagedObject as a new row in the DB?
There is no obvious way to just "copy" a NSManagedObject. The most robust way is to simply recreate everything from scratch.
Make sure you have all the changed attributes stored separately (here I am assuming they are in various text fields or that they are unchanged from the existing object). You can make this decision (new instance or not) when your editing view controller is dismissed:
if (![nameTextField.text isEqualToString:object.name]) {
ObjectClass *newObject = [NSEntityDescription
insertNewObjectForEntityForName:#"ObjectClass"
inManagedObjectContext:self.managedObjectContext];
newObject.name = nameTextField.text;
newObject.attribute1 = oldObject.attribute1;
// or
newObject.attribute1 = attribute1TextField.text;
// do this for all attributes
[self.managedObjectContext save:nil];
}

Approach for "calculated fields" on NSManagedObject subclass

I'd like to place some custom methods directly into each NSManagedObject. Think, "calculated fields": these methods provide read-only calculated values based upon persistent values on the Entity - which is identical to this question.
I'm using the Xcode New File... Wizard to create NSManagedObject subclasses for each of my Entities. I'm trying to benefit from the system auto-creating the accessors for me. For example:
Core Data Entity: "Site"
#interface Site : NSManagedObject
As I continue to add new Attributes to my Entities, I'm replacing their corresponding NSManagedObjects by using the Wizard. When each file is replaced, my custom methods are lost.
Should I create my custom methods elsewhere so that I can continue to leverage the Wizard? Or, should I keep the methods on the NSManagedObject and add Accessors for new Attributes manually? Is there another approach?
Create a category on your NSManagedObject subclass:
In the "New File ..." dialog, choose "Objective-C category".
Create a category "CustomMethods" on "Site".
Xcode will create files Site+CustomMethods.h, declaring the #interface Site (CustomMethods), and Site+CustomMethods.m for the corresponding implementation.
Add your custom methods to the category.
These files will not be overwritten when you recreate Site.m and Site.h in Xcode.
All category methods can be used as if they had been declared in the class itself. The only thing you can not do in a category is add new instance variables.
Once I have used the wizard to create the initial managed objects, I generally change them manually.
Another way of doing this is to create subclasses of the wizard generated class files and use these.
When they are regenerated, all of your custom code is in the subclass, as opposed to the overwritten class file.

Store per user data in sharepoint webpart

I got to store some user specific data (string) in my Sharepoint webpart.I guess hidden webpart properties should do the trick. Tried with the following attributes on webpart property:
[WebBrowsable(false)]
[Bindable(true)]
[WebPartStorage(Storage.Personal)]
[Personalizable(PersonalizationScope.User)]
But I observe in the webpart that data stored is not user specific. Any help?
How are you reading your data ? You can use the WebPart.PartCacheRead method to retrieve per use values.
web.config can be modified to signal how you want your WebPartCache to be stored. To support caching in database then you must include the [Serializable()] attribute to you custom classes
The WebPart.EffectiveStorage property can also help in determining how the data is stored:
if (EffectiveStorage == Storage.Shared)
{
output.Write("<b>You are now in shared mode.</b>");
}
Just wondering why you have WebBrowsable attribute set as false. Are the users not able to configure the web part ? When and how is the property value set ?

Resources