NSManagedObjects with like properties - objective-c

I have a UITableView that displays the value of a property called name of an NSManagedObject using CoreData. I have it working by just using a basic NSFetchRequest and then displaying the value of name in the UITableViewCell's textLabel.
However, many of the NSManagedObject's have the same name value, so I get duplicates in my table. How can I filter it so that I only have one of each name value?
Thanks for any help.

You can configure your fetch request to only return distinct values but that required that you return dictionaries instead of managed objects. Since you are asking for dictionaries you will have to specify what values to return.
You can see my answer to avoid duplicate results on Core Data fetch.
In short:
request.resultType = NSDictionaryResultType;
request.propertiesToFetch = [NSArray arrayWithObject:#"name"];
request.returnsDistinctResults = YES;

Related

Objective-C: How to get data from selected row in table?

I need to retrieve a string from data from a selected row in a table to compare it to a string.
I call
NSArray *values = [self.tableArrayController selectedObjects];
To get the objects at the selected row. However, values only has a count of objects of 1, and everything seems to be one big, long string of all my values. I cannot search through the values because I won't always know what to search for. I need a specific value (of which they are organized by columns).
I was hoping that the array would be of a certain count and I could retrieve the string based on the index of the object for the string.
How should I get all the different values (organized by columns) for a selected row?
EDIT:
I populate a NSDictionary (withmore values than that, this is just an example). The string value is the column identifier. The object is the object I'm adding that I want to appear on the table. This part below already works as I want it to:
NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:
object1, #"column",
object2, #"column2",
nil];
[self.tableArrayController addObject:dict];
Another way to get data from your table is to look at the "NSTableView" methods "selectedRow" & "selectedColumn".
With that information, you can get at the value you want directly via your array controller or you can use the NSTableViewDataSource method "tableView:objectValueForTableColumn:row:" to pass you back the NSString value of whatever it is that you're trying to get at.

Check if a specific data is into core data

Here's the question. I have a table where I save some data, which I get from a NSArray of NSDictionaries which will have the properties to be saved. Each dictionary in the array is a separated entity, so I loop the dictionary and save it using insertNewObjectForEntityForName to create diferent entity.
I need to refesh the data, but when is saving the data is duplicating the data that exist already in coredata. Im trying to check if the id exist on the core data using
for(NSDictionary *exist in dic){
if([[campaignDictionary objectForKey:#"element"] isEqualToString:#"id"]){
idToCheck = [dic objectForKey:#"value"];
}
}
if(table.campaign_id == idToCheck){
return exist = YES;
}
but its leaving the rest of the data without being checked, so only the id not being duplicated, any ideas to how approach this? thanks!
I decided it was best to delete all my content from Coredata before inserting the new content, thanks!!

NSFetchedResultsController add Objects manually

I recently stumbled across a difficult problem..
I'm normally using a NSFetchedResultsController to get Data out of my CoreData, and display it within a TableViewController.
The problem I have now is that I can't get the results I want with a NSFetchRequest right away, because having a m:n relationship and I implemented a Table in Core Data (References) which stores the references to the Objects...
So what I do now is, I use a fetch Request:
NSArray* references = [self.context executeFetchRequest:fetchRequest error:&error];
and then iterate through this array to get my Objects:
for(References* ref in references)
{
Post* tmp = ref.ref_of_post;
}
So is there a way to manually add these objects to my NSFetchedResultsController or is there a possibility to get the wanted object trough a NSFetchRequest?
Thanks for your help.
Never mind that.
I just added these Objects to a NSMutableArray and then would use this Array in the same way as the NSFetchedResultsController..

Distinct Object using NSPredicate

I have an NSArray of custom objects. Consider that the custom objects have a PageNumber property. I would like to filter my NSArray with a condition like "customObject.PageNumber is distinct".
I know I can loop through the array and eliminate object with duplicate pageNumbers. But is there any easy way to do it? I have tried,
[myarray valueForKeyPath:#"distinctUnionOfObjects.pageNumber"];
It is giving me the unique page numbers (like 7, 8, 9). But I want the custom object itself rather than just page numbers. Can any predicate help me?
I have created a simple library, called Linq to ObjectiveC, which is a collection of methods that makes this kind of problem much easier to solve. In your case you need the Linq-to-ObjectiveC distinct method:
NSArray* itemsWithUniquePageNumbers = [items distinct:^id(id item) {
return [item pageNumber];
}];
This returns an array of objects, each one with a unique page number.
Yes, that is possible with the help of NSPredicate
customObject=[(NSArray*)[myArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self.PageNumber==%d",pageNumber]] lastObject];
//pageNumber is an integer
The filtered array is an NSArray of your custom objects which is the result of filtering using the predicate. Since your page number is unique, it will return only an array of one object. We get that by passing lastObject message to it.
Refer:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/predicates.html#//apple_ref/doc/uid/TP40001798-SW1

traverse Core Data object graph with added predicate?

I want to load a client object and then pull their related purchase orders based on whether they have been placed or not, purchase orders have an IsPlaced BOOL property.
So I have my client object and I can get all purchase orders like this, which is working great:
purchaseordersList =[[myclient.purchaseorders allObjects] mutableCopy];
But ideally I would actually like 2 array's - one for each order type: IsPlaced=YES and IsPlaced=NO
How do I do that here? Or do I need to do another fetch?
First, there is no reason to be turning the set into an array unless you are sorting it and there is no reason to be turning that array into a mutable array. Did you get that from some example code?
Second, you can filter an array or a set by using a predicate so you can create two sets (or arrays) easily via:
NSSet *placed = [[myclient purchaseorders] filteredSetUsingPredicate:[NSPredicate predicateWithFormat:#"isPlaced == YES"]];
NSSet *notPlaced = [[myclient purchaseorders] filteredSetUsingPredicate:[NSPredicate predicateWithFormat:#"isPlaced == YES"]];
If you are wanting to use this for a UITableView then look into a NSFetchedResultsController instead. It will save you a LOT of boiler-plate code.
Do you remember what example code you got that from? Been seeing that -mutableCopy a lot lately and would love to quash it. :)