cs193p – Assignment #6 Extra Tasks #5 and #6

Please note, this blog entry is from a previous course. You might want to check out the current one.

Assignment #6 Extra Task #5

Adding the fetch for place and region information is going to break the way background fetching updates the app-switcher. Can you figure out why? Fix this problem. Hint: It is the background fetch completion handler that tells iOS to update the UI in the app-switcher. You can simulate a background fetch while running in the Simulator using the “Simulate Background Fetch” menu item in Xcode’s Debug menu.

… seems we took care of this problem … it’s working

Assignment #6 Extra Task #6

Switching to UIManagedDocument for our NSManagedObjectContext is going to break launching due to a background fetch event. Can you figure out why that is? Fix it. Remember that you can launch your application as if it were happening due to a background fetch by choosing Edit Scheme from the popup in the upper left corner of Xcode (near the run and stop buttons), then turning on “Launch due to a background fetch event” in the Options tab of the window that appears. You can also create a new scheme there that has that switch always on for easy background fetch launching during debugging.

… it’s also working …

What did we do wrong, that it works? We took care encapsulating Core Data requests (e.g. performing blocks in the managed-object context). We made sure that there is a single managed doucment which – while it is opening or getting created – is only accessed by a single process …

The complete code for all extra tasks is available on github.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

cs193p – Assignment #6 Extra Task #4

Please note, this blog entry is from a previous course. You might want to check out the current one.

Teach yourself how to use NSFileManager and use it along with NSData’s writeToURL:atomically: method to cache image data from Flickr (the photos themselves) into files in your application’s sandbox (you’ll probably want to use the NSCachesDirectory to store them). Maybe you keep the last 20 photos the user looks at or all the photos in Recents or maybe 50MB’s worth of photos? Up to you.

Create a new class with two public class methods. One will cache provided data for a given URL. The other one will return the cached image, if possible.

+ (void)cacheImageData:(NSData *)data forURL:(NSURL *)url;
+ (UIImage *)cachedImageForURL:(NSURL *)url;

Continue reading “cs193p – Assignment #6 Extra Task #4”

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

cs193p – Assignment #6 Extra Task #3

Please note, this blog entry is from a previous course. You might want to check out the current one.

If you were to use your application for weeks, your database would start to get huge! Implement a mechanism for pruning your database over time. For example, you might want to delete photos a week after you download them?

Add an additional date attribute created to the Photo entity to store when a photo has been added to the database. (Don’t forget to recreate its sub class).

Set the new attribute when a photo is added:

+ (Photo *)photoWithFlickrInfo:(NSDictionary *)photoDictionary
        inManagedObjectContext:(NSManagedObjectContext *)context
         existingPhotographers:(NSMutableArray *)photographers
               existingRegions:(NSMutableArray *)regions
{
    ...    
    photo.created = [NSDate date];
    ...
}

Continue reading “cs193p – Assignment #6 Extra Task #3”

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

cs193p – Assignment #6 Extra Task #2

Please note, this blog entry is from a previous course. You might want to check out the current one.

Loading Flickr information into your database can be ridiculously inefficient if, for each photo you download, you query to see if it is already in the database, add it if it is not (and maybe update it if it is). Enhance your application to make this download much more efficient, preferably only doing two or three queries total in the database for each “batch” of Flickr photos you download (instead of hundreds, which is what Photomania does). The predicate operator IN might be of value here.

Let’s start by limiting the photo queries, but only if there any photos. If there are none, the request containing a predicate with an IN operator and nil will most likely crash … A mystical helper method (described a little bit later) generates an array of photo IDs used to query the database. If there are matches, create a new array of Flickr photo dictionaries containing only photos not yet in the database. Replace the current photos array buy the new – reduced – one:
Continue reading “cs193p – Assignment #6 Extra Task #2”

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

cs193p – Assignment #6 Extra Task #1

Please note, this blog entry is from a previous course. You might want to check out the current one.

Still allow the user to force a fetch using the refreshControl in the appropriate table views. And use the cellular network to fetch only when the user prompts you via a refreshControl in this manner (otherwise restrict all your URLforRecentGeoreferencedPhotos fetches to WiFi only). If you are a bit stumped about how to know when to endRefreshing in a table view, think about whether there are some NSNotifications flying around that you could listen in on.

The refresh-control view is handled by the regions-table-view controller. The downloads are handled by the application delegate. To communicate between those two classes use notifications. The table-view controller will post a notification when a new cellular download should start. It will listen to another notification to know when the download has been finished. The application delegate will listen for the first notification and start the download. It will post the second notification when the download has finished.
Continue reading “cs193p – Assignment #6 Extra Task #1”

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

cs193p – Assignment #6 Tasks #9 to #11

Please note, this blog entry is from a previous course. You might want to check out the current one.

Assignment #6 Task #9

Do not store Flickr photos themselves (i.e. any image other than a thumbnail) in your Core Data database. Fetch them from Flickr on demand each time.

… well we don’t, so nothing to do there …

Assignment #6 Task #10

Your application’s main thread should never be blocked (e.g. all Flickr fetches must happen in a different thread). You can assume Core Data will not significantly block the main thread.

… we made sure of that earlier …

Assignment #6 Task #11

Your application must work in both portrait and landscape orientations on both the iPhone and the iPad and it must work on a real iOS device (not just the simulator).


… it worked for the previous assignment, we did not change anything which could make a difference … and we also checked …

The complete code for all mandatory tasks is available on github.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

cs193p – Assignment #6 Task #8

Please note, this blog entry is from a previous course. You might want to check out the current one.

Display a thumbnail image of a photo in any table view row that shows Flickr photo information. You must download these on demand only (i.e. do not ask for a thumbnail until the user tries to display that photo in a row). Once a thumbnail has been downloaded, you must store the thumbnail’s data in Core Data (i.e. don’t ask Flickr for it again). Don’t forget that table view cells are reused!

Because we have already prepared the Core-Data schema, and saved the URL for the thumbnail with the photo data, this ones is quite easy.

Set the image of cell to thumbnail stored in Core Data. If there is none, nothing will be shown, but we need to download it asynchronously. As soon as it has been downloaded store it in Core Data (in the corresponding context) and tell the view to update:
Continue reading “cs193p – Assignment #6 Task #8”

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!