Convert sqlite query into NSPredicate for core data - iphone

I am woking on a core data. I have one query in sqlite. Please help me how to write this for core data so that I can get the equivalent results.
SQLite Query: "select * from (select * from WORKDOCUMENT ORDER BY CREATEDDATE ) as WORKDOCUMENT GROUP BY DOCUMENTNAME"
Please help.
Thanks in advance.

I would suggest you break out the document name into a separate entity via a to-many relationship:
DocumentName:documents <-------->> name:WorkDocument
You can now simply fetch the DocumentName entity and then sort the result with this sort descriptor (tested):
[NSSortDescriptor sortDescriptorWithKey:#"documents.#min.createdDate" ascending:YES]

Related

Swift : Core Data join entity

I know that NSFetchedResultsController does not support multiple entity. But I would like to know what is the efficient way to solve the problem below.
Let's say I have two entity: catalogue and selectedProduct. I am fetching all the entity catalogue to the user so he can select some products the task is: only propose the products in the catalogue that are not in selectedproduct.
In SQL it will be like select * from catalogue where productName not in (select productName from selectedProduct).
In think the following predicate should filter the products as you require:
fetch.predicate = NSPredicate(format:"SUBQUERY(lescourses, $lc, $lc.listedescourses == %#).#count == 0",listdescourses)
I've used SUBQUERY. In theory it could be done with NONE or NOT ANY, but I sometimes find CD doesn't parse them correctly. Note that, as per comments, the FRC will observe only the changes to TblProduits, to if you were to modify an existing TblLesCourses object to establish a relationship to listdescourses, the FRC will not (unless you redo the performFetch) recognise that the related TblProduits objects should no longer be included.

how to sqlite query to core data

how can i translate the following sqlite query to core data
select name, firstname, class, telephone, entryDateTime, counselor, count()
from myTable
group by strftime('yy-mm-dd', entryDateTime), name, class, counselor
order by entryDateTime desc;
what did i try?
i want to group by date(yy-mm-dd), class, counselor, and name. entryDateTime is in UTC but it should be converted to local time.
thanks in advance
The most flexible method for "group by"-like queries is the NSFetchedResultsController. Please see my answer of a couple of days ago, which should help you.
To solve the problem of subgrouping based on more than one attribute, you just use the sectionNameKeyPath as a subgroup and do the other grouping from there after fetching.
Alternatively, consider putting the attributes you need into a different entity and group by that entity.

Retrieving a HashMap

A entity containing a HashMap (annotated with ElementCollection) will be persisted using Eclipse Link / JPA.
By using the following JPQL query the HashMap should be retrivied now:
SELECT t.myMap FROM myEntity t WHERE t.id = :id"
Unfortunately the result is not the Map again, but a list of Strings which are (only) the values of the HashMap.
How can I get the HashMap as a single result?
Any help is appreciated.
You can't. What you are asking for is not the collection in the entity, but elements from the join. The best you might get is to query for the entity and use its getMyMap() to get the collection you are after

iPhone: CoreData join

My question similar to question iPhone CoreData join. The difference is that I need to get all LanguageSets from a database for a given category.categoryName. How predicate will looks like ? Thanks...
I assume its the same schema an you are fetching LanguageEntry. You can assign a predicate to the fetch request like
NSPredicate *predicate=[NSPredicate predicateWithFormat:#"category.categoryName==%#",<given name>];
Since you want only the LanguageSet , you should specify so in the fetch request
[request setPropertiesToFetch:[NSArray arrayWithObject:#"languageSet"]];
Don't forget to set resultType as NSDictionaryResultType.

Unique Values from Core Data

I have a core data-based app that manages records of auto dealerships. Each record stores the dealer's address, which is broken into addressLine1, addressLine2, city, state, and zip components, each stored as a string in the data store.
I would like to present a list of cities with dealerships to the user, so I'm trying to figure out if it is possible to get a list of every unique city name that has been entered into the store. I other words, is it possible to issue some sort of query against all of the dealership records that will return a list of distinct city names?
I would know how to do this easily with a SQL query, but (how) is this done in Core Data?
Thanks very much!
Core Data have the option to get distinct record. The method of getting unique results using NSArray and NSSets are not recommend.
[fetchRequest setResultType:NSDictionaryResultType];
NSDictionary *entityProperties = [entity propertiesByName];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:[entityProperties objectForKey:#"<<yourattrib>"]]];
[fetchRequest setReturnsDistinctResults:YES];
Refer Apple documentation and check the answers for How to fetch distinct values in Core Data?
You're right, there isn't an "easy" way to do this with Core Data, because Core Data is not a database. However, it is possible. Here's the general idea:
Fetch all your Dealer objects via an NSFetchRequest. To simplify the query, you can set the fetch request to only fetch the city attribute.
Execute NSArray * uniqueCities = [fetchedDealers valueForKeyPath:#"#distinctUnionOfObjects.city"];
A quick way to ensure a unique set of things is to use NSSet. Once you have the results for a query on city take your NSArray and do
NSSet* uniqueResults = [NSSet setWithArray:resultsArray];
You can transform the set into another collection class if more convenient or just the object enumerator to do something with all of them. I do not know if this approach or the valueForKeyPath method from Dave DeLong is more efficient.