Debugging Core Data managed objects with predicates - iphone

My application has the data models a little bit complicated. I need to debug a fetch request with different predicates.
Is there any fast way to see different results for different predicates? I am tired with changing only one predicate and I have to start again my navigation application with nearly 10 steps before.
An example of these predicates that I would like to see the results:
item = %#
item = %# AND quantity = %#
item = %# OR (startdate >= %# AND enddate <= %#)
etc...
As using Core Data, I can not see the database with its' values to do some SELECTs.

You can add logic in your code to change the predicate and fetch again (maybe add a temporary button to trigger this and cycle through your various predicates).
You may also be interested in viewing the data in your SQLite file. Check out this answer to How view data stored in Core Data?

I'm not sure if it is any help, but if you want to quickly see the return results, go into gcc command line and write
po <name of array with results>
so if the array is items
po items
Will give all the returned results printed nicely in the console

To view your data inside of SQLite for free, just get FireFox. Then install SQLite Manager.
Cheers.
-RoLYroLLs
http://iphone.rolyrolls.com

Related

Loop through database ANYLOGIC

In my model I want to loop through the database which contains multiple columns (see example) by an event. The idea behind it is that I want to create dynamic events based on the rows in the database.
But I've no clue how to iterate through a database in anylogic and also was not able to find an example of a loop with a database.
The dummycode of my problem would look something like this:
For order in orderdatabase:
Create order based on (order.name, order.quantity, order.arrivaltime, order.deliverylocation)
Where order in the loop is every row of the database, and the value on which the creation is based based on the different column values of that specific row.
Can somebody give me a simple example of how to create such a loop for this specific problem.
Thanks in advance.
Use the database query wizard:
put your cursor into a code field
this will allow you to open the database wizard
select what you need (in your case, you want the "iterate over returned rows and do something" option
Click ok
adjust the dummy code to make it do what you want
For details and examples, check the example models and the AnyLogic help, explaining all options in detail.

swift : get the closest date stored in the core data

I have two data in core data.
Entity Name = "Contact", Attribute Name = "date"(selected datePicker.date), "content"(textFeild.text)
It is that sort of information in a table view, was a success. I want to show only one content in the another view controller. The data of the closest time to the current time.
I was asked to help with the following code.
var stores: Contact?
if ((stores?.date!.timeIntervalsince1970 ?? 0) > NSDate().timeIntervalSince1970) {
labelName.text = stores?.content!
}
But the label has not been any indication on. And no error... What's the problem?
As you mentioned using Core Data, the right direction is to Fetch this objects (the contacts). Then you will have an Array of NSManagedObjects. If the fecth is successful you iterate them in a ´for in´ loop.
Inside this loop use ´.valueForKey´ to extract the dates and apply the rules you have created, returning the NsManagedObject or Atribute you want.

Swift: Core Data - Resolving a logic issue with object creation

So, my Swift app allows a user to choose sports teams to see historic match information for. Currently, a user selects team(s) and the JSON data file of historic matches is scanned.
If a historic match includes a name of a selected team, the details of the match are stored in a Core Data entity, which is fed into my main Table View.
However, this presents an issue I can't get my head around solving.
If a user selects team A and B, and the database contains a match where team A and B played EACH OTHER, two objects for the match details are created, and as such, Table View cell is created twice, once for team A being found in the instance of the match, and again for team B.
Is there an easy and efficient way to trim any duplicates caused in this way? I don't know whether to handle this at the object creation time, or just to find a way of removing any duplicated cells from my Table View.
Thanks so much.
I think you should redesign your setup. Have all the records to be searched stored in Core Data.
If you have a hardcoded JSON file - import it on first start. If you have retrieved JSON - insert / update the elements that are new / changed in your Core Data object graph.
You would have a Match or Game entity and it would be retrieved only once. The fetch predicate would be something like
NSPredicate(format: "homeTeam = %# || guestTeam = %#", selectedTeam, selectedTeam)

Saving to CoreData - Add to Top

All,
Is it possible to save to the top of the CoreData? If so, how?
For example, every time something is added to the data store, I want it to be inserted at the top. This way, when the results are fetched they would come back sorted by most recent first without having to save the NSDate and fetch with a predicate. Here is a crude example:
Most recent
Earlier
Yesterday
Last Week
Thanks,
James
What is the "top"?
Core Data does not assign any particular order to the objects it stores. If you want to impose some order on the objects, add an attribute to the entity that you want to be ordered and then sort on that attribute when you fetch the objects. So, you could add a serialNumber attribute that always increases. Sorting on that serial number would order the objects.
Add a creationDate in the model, and the following code to your custom implementation of the object:
- (void)awakeFromInsert {
[self setPrimitiveCreationDate:[NSDate date]];
}

Optimizing text searching using NSFetchedResultsController & Core Data

I have a tableview with a NSFetchedResultsController data source, displaying a list of names from the underlying Core Data SQLite store. I have implemented a search bar. When the first character is entered in the search bar, a fetch request in the following form is executed:
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:#"name beginswith[cd] %#", searchText];
However, when the second search character is entered, I would like to filter the fetched objects of the fetchedResultsController, rather than executing another fetch request (implementing another fetch request, similar to the 1 above, results in another trip to the store which I had hoped would not occur as the results of the second fetch would just be a subset of the first). Is there anyway to filter the fetchedResultsController so that another trip to the store is avoided?
Yes, set up a search "state" and then switch your NSTableViewDatasource to point at an array that is filtered from the -fetchedObjects returned from your NSFetcResultsController.
You can then update the filter on that array as the user types more information in and it will not go back to the store. This will even allow you to filter on the first character entered and avoid even that unnecessary trip to the store.
You could always store the results of the first fetch into an array and when text in the search bar changes, filter the contents of the array with another predicate.