DPS923 notes – Tue Mar 17

General interest and assignment-related topics.

.

Planned topics for today

Standalone fetch request (not embedded in a fetched results controller)

Fetch data from web service and save on device

Scrollable view

.

Standalone fetch request

fetched results controller object is used with a table view.

If you are not using a table view, how do you query the on-device data store?

With a fetch request object.

Previously, you have learned that a fetched results controller includes a fetch request property.

You can use a fetch request object to perform a get-allget-some-filtered, or get-one query. For all of these situations, the results come back as an array that has zero or more objects.

A get-all query will not need a predicate.

A get-some-filtered or get-one query will need a predicate. Predicate string format examples are fully documented here (although some will not work with a Core Data store).

For any query that will return a collection, you can choose to configure sort behaviour, if you wish.

.

Follow this guidance to code your first standalone fetch request. In this scenario, we are fetching a ‘Sport’ object that’s in the Winter 2015 programming Assignment 1. We are using a get-one predicate, so the fetched results will be an array with exactly one object in it.

For best results, in your Model class, create a method that will accept a fetch request argument, execute the fetch request, and deliver the results. (We do this because controllers do not have direct access to the Core Data stack.)

Here’s how to create a fetch request object:


let f = NSFetchRequest(entityName: "Sport")

Next, ask yourself whether you need a predicate, or sorting.

In our situation, we do need a predicate. We can configure it like this:


f.predicate = NSPredicate(format: "hostId == %@", argumentArray: [sportId])

.

Here’s what the ‘execute fetch request’ in the Manager class looks like:

execute-fetch-request

.

Fetch data from web service and save on device

One of the Assignment 1 tasks is to fetch data from a web service, and save the data on the device.

Today’s code example, Toronto2015, is in the GitHub code repository. Download it, study it, and use some of its code and principles to enable you to add functionality to your app.

Your professor will guide you through the topic.

.

How to design your coding plan

The code example is fully-commented, and lengthy, mostly because of the comments.

Most of the logic is in the ‘launch’ controller, in the Launch.swift source code file.

It relies on functionality in the Model class, and in a new ‘extension’ that’s in the Extensions.swift source code file. Learn more about extensions in the Swift Programming Guide section.

What is the coding plan?

Well, it has a number of steps, summarized below:

In the launch controller, check if the data store has any objects in it.

If not, then register for a ‘fetch sports’ notification.

Then send a request for ‘sports’ to the web service, and wait for the results.

When the response comes back, it calls the ‘fetch sports completed’ method.

Next, register for a ‘fetch venues’ notification.

Send a request for ‘venues’ to the web service, and wait.

When the response comes back, it calls the ‘fetch venues completed’ method.

At this point, you have the data.

Add ‘sport’ objects to the device store, in a loop.

Then, add ‘venue’ objects to the device store, in a loop.

While adding each ‘venue’ object, must set the relation to the ‘sport’ object(s).

.

Scrollable view

Used for content that is taller than one iPhone screen.

A “Scroll View” can be added to a scene that’s managed by a standard ‘view controller’ (UIViewController). However, working with a scrollable view on the storyboard requires some configuration.

Here’s a step-by-step guide to adding and configuring a scroll view.

.

1. On the storyboard, show the document outline

It’s the icon in the lower-left area of the storyboard.

document-outline

.

2. Select and size the View Controller item in the document outline

First, select the View Controller item in the document outline.

document-outline-select-view

.

Next, show its Size Inspector.

Change the Simulated Size to Freeform. Then, enter your desired size. An iPhone 4 or 5 screen is 320 points wide, and 480 or 568 tall (and iPhone 6, 6 Plus, and iPads are taller than 480 points). So, you can enter a new height value, for example 900.

view-controller-size-adjust

.

At this point in time, the View Controller scene on the storyboard will resize itself, to the new height.

.

3. Add a Scroll View to the scene

From the user interface Object Library, locate the Scroll View item. Select it, then click-drag-and-drop it on to the view. Be careful where you place it. Use both guides (horizontal and vertical).

.

4. Add a View to the scene

From the user interface Object Library, locate the View item.

view-in-object-library

.

Select it, then click-drag-and-drop it on to the scroll view. Be careful where you place it. Use both guides (horizontal and vertical).

.

5. For the view (added in step 4), pin its height

Select the view that you added in step 4.

view-pin-height

.

On the Editor menu, choose Pin, then Height.

.

6. For the view (added in step 4), resolve auto layout issues

Again, select the view that you added in step 4.

On the Editor menu, choose Resolve Auto Layout Issues, then in the bottom “All Views” area, choose Add Missing Constraints.

A number of ‘constraints’ will be added to the document outline hierarchy.

.

7. Add your desired user interface objects and controls

Now, add labels, text fields, buttons, and so on to the view that was added in step 4. That view is the ‘container’ view for our user interface objects and controls.

After you add some items, look at the document outline, and the scene. It will look something like this:

tall-view-configured

 

.

As you have done before, you may have to select the top-level View object (in the Document Outline), and Add Missing Constraints from the lower-right area of the storyboard editor.

.

8. Build and run

At this point in time, you should be able to build and run, and scroll the view up-and-down.

This task can be picky and challenging, so be prepared to modify your technique.

See the “Scroll” code example on the GitHub code example repository, in the Week_09 folder.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: