CoreData Tutorial Part 5/5 - Model Versioning, Lightweight Migration, Model Mapping
Steps in part 5 :
Add New Managed Object Model Versions
Set Lightweight Migration
Add Model Mappings
Test
When you need to change your Core Data Entities or structure, you have to add new model version before you send your App in AppStore. If you change just attributes or fetched templates, it’s not need, you can do it without consequence. But if you want to add a new Entity, you have to migrate your persistent store to a new model versions. You should also set mappings for the migration. In this tutorial we add model versions, model mappings, and using Lightweight Migration.
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
âś“ Live Streamingâś“ Interactive Chatâś“ Private Showsâś“ HD Quality
Anya is LIVE right now
FREE
Free to watch • No registration required • HD streaming
CoreData Tutorial Part 5/5 - Model Versioning, Lightweight Migration, Model Mapping
IOS 8.3 Xcode 6.3.
Before you start, here is the first and second part of this series:Â
Core Data tutorial Part 1 / 5 - Create Core Data Managed Object Model with Attributes, Relationships and Delete Rules
Youtube Video Part 1
Core Data tutorial Part 2 / 5 - Save Entity’s Attributes, Relationships, and make FetchRequest
Youtube video for part 2
Core Data tutorial Part 3 / 5 - Edit, Delete Objects
Youtube video for part 3
Core Data part 4 / 5 - Change Core Data model, using SortDescriptor, NSPredicate FetchRequest and filter Array using Predicate
Core Data Part 4.2. / 5 -Â Using NSFetchedResultsController, Reorder/Moving Table View Cells with Long Press Gesture, and update immediately Core Data displayOrder attribute
Youtube video for part 4
Check my article: “You must to know! Collection - Core Data simple explanations”Â
Steps in part 5Â :
Add New Managed Object Model Versions
Set Lightweight Migration
Add Model Mappings
Test
When you need to change your Core Data Entities or structure, you have to add new model version before you send your App in AppStore. If you change just attributes or fetched templates, it’s not need, you can do it without consequence. But if you want to add a new Entity, you have to migrate your persistent store to a new model versions. You should also set mappings for the migration. In this tutorial we add model versions, model mappings, and using Lightweight Migration.
1. Managed Object Model Versioning
If you learn CoreData, I’m sure, you use the “delete App in Simulator before Run” method, when you change your Database and get crash when try to run your App in IOS Simulator or on your IOS device. If you work on a real App, and you want to submit it in App Store, you have to use this easy Managed Object Model Versioning method.
1.1. Add new Model
Select your CoreDataTutorial.xcdatamodeld file in your XCode, In “Editor” menu select the “Add Model Version…” submenu.
Xcode add Version name with number 2 automatically. If you use these version names in the future, you can see the changes step by step. The important thing is, the “Based on model” select Field should selected the correct actual model version.
1.2. Select the CoreDataTutorial.xcdatamodeld group, and at the right side set the new version as a current model version
1.3. Add new Entity named: “Migration”, and a new attribute: “teststring” as a String attribute.
1.4. In Editor menu **click on **“Create NSManagedObject Subclass..”. In the pop-up window select the current model versions, than thick only the “Migration” Entity. Not select the “scalar properties for primitive data types”.
2. LightWeight Migration
For Model Versioning and for Model Mapping you need to use Lightweight Migration. This will be necessary for iCloud sync too.
In Appdelegate.m file created you the NSPersistentStore. Here you need to use NSDictionary to use NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption Both of them are YES. In this case, the first line help Core Data automatically migrate the lower versioned stores to the latest model. NSInferMappingModel help to Core Data to infer automatically the attributes of source model entities as attributes in the destination model entities. Â
You can add these options to your PersistentStore with this simple code: Using those two options together is called lightweight migration.
You should change your - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { part of code for this with this NSDictionary line and addPersistentStore “options:nil” need to change to “options:options”:

Run the App and create some User with type and Hobby:
3. Model Mapping
When you want to say manually, which data from the earlier Model should goes which attributes to the new current model versions, you need to creata  Model Mapping and change NSInferMappingModelAutomaticallyOption from YES to NO. Start with this.
**3.1. **Add a new Model Version and a new Entity
Add a new Model Version: CoreDataTutorial 3
Set the Model 3 to the current Model. Â
Add New Entity: MappingHome with an attribute: “hmname” as a String attribute.Â
Create NSManagedObjectModel Subclass…
**3.2. **Create Model Mapping
Select Data Model group, right click, New File and select Mapping Model:
Set CoreDataTutorial 2 as** Source Data** and** CoreDataTutorial 3** Model version as Target Data Model:
Save with the name: “Model2to3”:
Now you have a Mapping Model:
3.3. Set Source and Destination Data:
Click on Type Entity, and check the right side at the Entity Mapping the Source and Destination Entity names. These are the same. At the typeName attribute there is a Value Expression: “$source.typeName”, that means, the Destination typeName attribute use the typeName attribute from the source Model’s Type Entity.
Now click on your new Entity. You can see, that MappingHobby hasn’t got Value Expression, that means, no source Data.
But we would like to see here, the old Hobby Entity’s hobbyName attribute data.
Select MappingHobby Entity, at right side select “Hobby” as a Source. Now Your Entity Name chnaged from “MappingHobby” to “HoobyToMappingHobby”.
Now Add source Value Expression to the “hmname” attribute: “$source.hobbyName”.
4. Test Model Maping
Yes, we should test it. Â Go to
** ViewController.m**
, import
“MappingHobby.h”
file.
in ViewDidLoad create a FetchRequest for the MappingHobby Entity, and List in NSLog all of the string attribute “hmname” strings, which should be the name of the Hobby Entity. So we call them too:
Run Project —> Clean, than Run the App, and check the log, you should see the Fetched MappingHobby Object names as same as Hobby Object names.
Now you are ready to use Model Versioning with Lightweight Migration or create Model Mapping.Â
NEXT
Download the Xcode template from Github
The next part of the series: soon. (Export - import data, migration.)
If you would like to be notified of my new solutions, pleaseÂ
Create Storyboard with 2 ViewController and add NavigationController too.
Set NavigationBar colors, clear background image, set your own separator line with an image.
Create Navigation Bar UIBarButtonItems programatically.
Change UIBarButtonItems position
Manage NavigationBar: show / hide switch between ViewControllers - Appear with animation
Tip: Hide StatusBar
1. Create Storyboard with 2 ViewController and add NavigationController too.
1.1. Do these first steps:
Storyboard Basic Settings Part 1. - New ViewController - New CocoaTouch files - Embed NavigationCotnroller - Add UIBarButtonItem
**2. Set NavigationBar colors, clear background image, set your own separator line with an image. **
2.1. change barTintColor and tintColor
Basicly your NavigationBar background color = barTintColor is gray, and your buttons text color = tintcolor is blue.
You can change this with these codes to white backgroundcolor and black tint:
Run your App in Simulator and check it:
2.2. Remove/Clear UINavigationBar background image - totally transparent NavBar If you would like a totally transparent Navigation bar, just do it with this code:
_Test ** 1. **Â If you would like to test it, first drag & drop UITableView and Table View Cell from your Objects library to your first ViewController._
Enter tableview Identifier: “cell”.
_**2. **Create IBOutlet - so ctrl drag to the ViewController.h file the TableView, and add name e.g. “MyTableView”. (use “show the Assistant Editor button to reach the .h file at the right side)__ _
_Don’t forget add UITableViewDelegate and UITableViewDataSource too here. And synthesize it in .m file.** **_
_**3. **Put the numberOfSections and numberOfRows DataSource info: **
_**4. **Put the tableView cellForRowAtIndexPath: too with these simple datas:
_5. In ViewDidLoad delegate the tableview, and reload Data. Now your code looks like this:__ _
_**6. **Run your Simulator and try to scroll the TableView. Your NavBar white, and you can’t see the Rows._
_**7. **Now, use the code in your vViewDidLoad:
_Run your App again, and now you can see the cells behind the NavBar: _
But you haven’t got separate line anymore. So create it!
2.3. Create your own separator line with UIView
Put your image (create it yourself) into your supporting folder:
In your ViewDidLoad use this code:
Run your App again. Now, you have a great line :)
Create Buttons in your ViewController.h file, and synthesize them:
Add your images to your Supporting Files:
3.1. Create Buttons and add as an Array to the NavigationBar with this code in your ViewDidLoad:
3.2. And you can create a selector, when you press e.g. one of them, and perform a segue:
Check you Main.storyboard, you should have a segue with this identifier - if no, just add the name now:
Run your App and Test it:
4. Change UIBarButtonItems position
4.1. You can change the NavigationBar UIBarButtonItem’s position, if it is an image. Because in this case, you can use UIEdgeInsetsMake
This is just one line, and some test and Math, you can position your buttons e.g. to the ends of your separator line.
4.2. Just run your simulator, check i, and modify the numbers and check it agin and again, maybe it will be good:
5. Manage NavigationBar: show / hide switch between ViewControllers - Appear with animation
You can use these 3 method: ViewWilAppear, viewWillDisAppear and viewWillDidAppear to manage this animation:
Copy this 3 line into your SecondViewController.m file too.
You can change the animated: NO to YES and back, Â and you can see the difference:
First case - animated: WillAppear - YES, WillDisAppear - NO, WillDidAppear - NO:
Storyboard Basic Settings Part 1. -Â New ViewController - New CocoaTouch files - Embed NavigationCotnroller - Add UIBarButtonItem
Steps:
Create the project and add a new ViewController
Create New ViewController and subclass
Create Show Segue from FirstView to SecondView
Embed Navigation Controller, add UIBarButtonItem
Run your App in your IOS Simulator
1. Create the project and add a new ViewController
1.1. Create new Project - choose Single View  type, and add name, select Objective-C language and do not check the “Use Core Data” checkbox.
1.2. click on Main.storyboard, and click on ViewCotnroller, and change the simulated Metrics to iPhone 4.7-inch for example, so you can manage your ViewControllers on your monitor :)
2.1. Drag & drop a new ViewController to the Storyboard, and change the simulated metrics too.
2.2. Right click on your Files and select New File menu…
…select Cocoa Touch Class…
…add a name…
… and the result: you have new h and m file as a viewcontroller subclass.
2.3. Enter the new class reference for the second View Controller. So all you're writing in these files will happen with this ViewController of the storyboard.
Click on your Main.storyboard, select the second ViewController.
Add the class name in the CustomClass Field:
Add the same name as Storyboard ID, and check the “Use storyboard ID” checkbox at the Restoration ID Field.
Core Data tutorial part 4.2 / 5 - Using NSFetchedResultsController, Reorder/Moving Table View Cells with Long Press Gesture, and update immediately Core Data displayOrder attribute
IOS 8.1 Xcode 6.1
The full tutorial with explanations
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
âś“ Live Streamingâś“ Interactive Chatâś“ Private Showsâś“ HD Quality
Anya is LIVE right now
FREE
Free to watch • No registration required • HD streaming
Core Data tutorial part 4.2 / 5 - Using NSFetchedResultsController, Reorder/Moving Table View Cells with Long Press Gesture, and update immediately Core Data displayOrder attribute
IOS 8.1 Xcode 6.1.
Before you start, here is the first and second part of this series:Â
Core Data tutorial Part 1 / 5 - Create Core Data Managed Object Model with Attributes, Relationships and Delete Rules
Youtube Video Part 1
Core Data tutorial Part 2 / 5 - Save Entity’s Attributes, Relationships, and make FetchRequest
Youtube video for part 2
Core Data tutorial Part 3 / 5 - Edit, Delete Objects
Youtube video for part 3
Core Data part 4 / 5 - Change Core Data model, using SortDescriptor, NSPredicate FetchRequest and filter Array using Predicate
Youtube video for part 4
Check my article: “You must to know! Collection - Core Data simple explanations”Â
Steps in part 4.2Â :
Set NSFetchedResultsController, and do changes in the code, create displayOrder attribute.
Using Long Press Gesture to Reorder Table View Cells
Updating Core Data displayOrder attribute when reorder cells
Test
Our goal:
Youtube video for this tutorial!
First of all:
Why you need to use FetchedResultsController, and not use just simple FetchRequest? Maybe after edit your attributes, your UITableViewController not update right away. Maybe you have a very big database, and you would like to fetch just those, which show for the user on the screen, and when the user start to scroll you can fetch more data (setFetchBatchSize code). Or maybe you have other reason.
Please read the entire article, but at least this small part:
You use a fetched results controller to efficiently manage the results returned from a Core Data fetch request to provide data for a UITableView object.
While table views can be used in several ways, fetched results controllers are primarily intended to assist you with a master list view. UITableView expects its data source to provide cells as an array of sections made up of rows. You configure a fetch results controller using a fetch request that specifies the entity, an array containing at least one sort ordering, and optionally a filter predicate. The fetched results controller efficiently analyzes the result of the fetch request and computes all the information about sections in the result set. It also computes all the information for the index based on the result set.
In addition, fetched results controllers provide the following features:
Optionally monitor changes to objects in the associated managed object context, and report changes in the results set to its delegate (see The Controller’s Delegate).
Optionally cache the results of its computation so that if the same data is subsequently re-displayed, the work does not have to be repeated (see The Cache).
A controller thus effectively has three modes of operation, determined by whether it has a delegate and whether the cache file name is set.
1 No tracking: the delegate is set to nil. 
The controller simply provides access to the data as it was when the fetch was executed.
2 Memory-only tracking: the delegate is non-nil and the file cache name is set to nil. 
The controller monitors objects in its result set and updates section and ordering information in response to relevant changes.
3 Full persistent tracking: the delegate and the file cache name are non-nil. 
The controller monitors objects in its result set and updates section and ordering information in response to relevant changes. The controller maintains a persistent cache of the results of its computation.
If you are planning to develop a larger app with more Entities and relationships and attributes, then you have to read all about NSFetchedResultsController, and must use the settings in the right way.
The starting point is the 4. part of this Core Data tutorial.
1. Set NSFetchedResultsController
1.1. In MyTableViewcontroller.h file set NSFetchedResultsControllerDelegate method, and create NSFetchedResultsController property, and in.m file synthesize it:
 MyTableViewcontroller.h:
MyTableViewController.m
1.2. Create the fetch request with NSFetchedResultsController.
You must to use SortDescriptor, when you use NSFetchedResultsCotnroller. For example you can tell that you want to order your fetched data ascending by your “displayOrder” attribute.
You can use setFetchBatchSize, after then create NSFetchedResultsController. At this point, we just put nil to key Path, because we use just single section in our UITableView. And I don’t want to use Cache, so I put nil to this part too.
 IMPORTANT
If you are using a cache, you must call deleteCacheWithName: before changing any of the fetch request, its predicate, or its sort descriptors. You must not reuse the same fetched results controller for multiple queries unless you set the cacheName to nil.
1.3. Implementing Table View Data source method
Simply put this code to your MyTableViewController, then when you insert or delete or change your TableView, the FetchedResultsController will do the updates. So you can use fewer code in the future to do these things, because the controller will do it for you.
But there will be a big problem. We call sortDescriptor with “displayOrder” attribute, but in our model, we haven’t got displayOrder attribute yet. So we have to change our model:
1.4. Create displayOrder attribute for User Entity. Type: “Integer 32” Type, “displayOrder” name.
You can change model in earlier parts, so you can do it, just create the attribute, after then delete the User.m and .h file, and create them again (CreateManagedObjectSubclass). And of course you can delete the app in the Simulator.
We can use perform fetch, and nil the controller, and then set the myArray with the fetchedObjects of our controller:
Now you can test your app, it will work. But, you not add displayOrder attribute to the user object, therefore your app can’t order them by “displayOrder” attribute. It’s logic. So we can add the number for every user when we create them.
1.6. Set displayOrder value for user:
Using this 2 line of code in your ViewController.h file in prepareForSegue block before NSError *error line:
So now, your ViewController.m file prepareForSegue code looks like this:
  In MyTableViewController.m file, in tableView cellForRowAtIndexPath, change your cell.textLabel.text to show the displayOrder number before the user name. Change your line to this:
 1.7. To clean your all old code, you need to change the UITableViewCellEditingStyleDelete method.
Because Controller will do the “deleteRowsAtIndexPaths” line, so you should delete here this line, and also you have to change the self.myArray = [appDelegate getAllUserRecords] code to this:
 2. Using Long Press Gesture to Reorder Table View Cells
When you would like to reorder your TableViewCells without editing mode, you can use this perfect tutorial at raywenderlich.
As I know this code updated, because there was more bugs on IOS8, but me and some other developer try to find solutions, (you can see in comments), so finally, it works fine! (I didn’t check the updated code, but I use this, as I wrote in the comment, so you can copy this).
And please read the article to understand how this code works!
2.1. First, we create a Gesture, but you need delegate method too.
Put in your MyTableViewController.h file the UIGestureRecognizerDelegate to your interface, and this SnapshotView code to call:
- (UIView *)customSnapshotFromView:(UIView *)inputView;
 2.2. In your viewDidLoad in MyTableViewcontroller.m file, set your TableViewEditing mode to No, Scroll enabled YES, and Create the UILongGestureRecognizer:
 2.3. Put the moveRowAtIndexPath, canMoveRowAtIndexPath, editingStyleForRowAtIndexPath, and shouldIdentWhileEditinRowAtIndexPath codes.
You can read more about these here!
2.4. Put the raywenderlich code and gesture settings, and Snapshot view helper to the bottom of your MyTableViewController.m file: (this is my final code, what I use, maybe not entire same which is on the raywenderlich website)
2.5. Test your App, Run, and try to drag and reorder your cells. It woks fine! Great! But you can see, that your “displayOrder” number not change, because the new reorder state not saved into Core Data.
3. Save changed “displayOrder” into Core Data, so save the new order
 3.1. Use this short code in your handleLongPress: block in UIGestureRecognizerStateChanged: case before the “break” line.
 should looks like this:
  But it will be not perfect!
You need to use _fetchedresultcontroller.fetchedObjects everywhere in the TableView.
 3.2. change self.myArray to _fetchedResultsController.fetchedObjects in your .m file.
in viewDidLoad delete or comment out this: //self.myArray =[_fetchedResultsController.fetchedObjects mutableCopy];
in tableView numberOfRowsInSection: use this code instead of “return myArray.count”:
in tableView cellForRowAtIndexPath: change User *user1 = [self.myArray to this:
User *user1 = [_fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
in didSelectRow block also need to change myArray
and you should put [self.tableView reloadData]; line into fetchedResultsController block before the last “return _fetchedResultsController” line.
These little things need to everything works together in a right way! If you didn’t do these, but use the method which will reorder your data and save into core data database, it will show that numbers change, but position not, or position change but numbers no, and other types of mistakes.Â
3.3. Create userchangeArray NSMutableArray:
In MyTableViewController.m after interface create the MutableArray, and set as _FetchedResultsController.fetchedObjects mutbalecopy top of your file:
3.4. Set the reorder method in handleLongPress: block case UIGestureRecognizerStateChanged:
 4. Test
Now you can run your App in simulator, and you can reorder your rows, and you will see, that your numbers change too as a new position of the cell/data.
If you use just commitEditingStyle UITableViewCellEditingStyleDelete - maybe now it not works, I suggest to use some of Swipeable Table View Cell to use this function, and create own gesture settings. (I will create a tutorial for this).
NEXT
Download the Xcode template from Github
The next part of the series: soon. (Export - import data, migration.)
If you would like to be notified of my new solutions, pleaseÂ
Steps in part 4: 1. Create Hobby Entity - change Core Data model 2. Create and Save hobbies to user. 3. SortDescriptor and NSPredicate Sorry for the end of the video, i finally did product -clean, and then it worked fine. But i just present the way of using filteredArray with Predicate. The correct, best solution at this App to using the simple setPredicate in AppDelegate FetchRequest.! Full tutorial here! Xcode template file on Github here
Core Data part 4 / 5 - Change Core Data model, using SortDescriptor, NSPredicate FetchRequest and filter Array using Predicate
Before you start, here is the first and second part of this series:Â
Core Data tutorial Part 1 / 5 - Create Core Data Managed Object Model with Attributes, Relationships and Delete Rules
Youtube Video Part 1
Core Data tutorial Part 2 / 5 - Save Entity’s Attributes, Relationships, and make FetchRequest
Youtube video for part 2
Core Data tutorial Part 3 / 5 - Edit, Delete Objects
Youtube video for part 3
Check my article: “You must to know! Collection - Core Data simple explanations”Â
IOS 8.1, Xcode 6.1
Steps in part 4:
Create Hobby Entity - change Core Data model
Create and Save hobbies to user.
SortDescriptor and NSPredicate
1. Create Hobby Entity - change Core Data model
1.1. Add New Entity, Name: “Hobby”, and add attribute: “hobbyName” as String, and then create relationship: “userofhobby” destination:User, type: to One.
1.2. Add the “hoobiesofuser” relationship to the User entity, set the Destination to Hobby, set the inverse, as “userofhobby”, finally set type to “to many”.
1.3. Now, you must to update your Core Data model. You have 2 ways to do this.
1. case you should just delete the entity’s earlier .h and .m files, and delete the App from the simulator. In this case you just work on the project, but this App not available in the App Store, so you do the changes just for yourself. We do now this.
2. case you have to do migration, before send the App to the Appstore. We will do in the fifth part of this series.
So just Delete (Move to trash), and then Editor - Create managedObject again
1.4. Open Simulator and delete the App. After that run the app, test that it works again. Create some user with type, and save them.
2.1. Create a new TextField on storyboard, check delegate!, and declare in ViewController.h file property, and synthesize in .m file, and import Hobby.h file in your ViewCotnroller.m.
2.2. Set the Textfield3 properties as the TextField1 and 2. You should just copy them.
looks like this:
2.3. Change the prepareForSegue code to save the hobby for the user. Set hobby1 string with TextField3.text, and add to the user.
2.4. In MyTableViewcontroller.m import Hobby.h file, set the cell.detailTextLabel.text to show on first place the typeName string, and the second place the hobbyName string. You have to create an Array for the user1’s hobbies, and then from that detect hobby1 object, and use the hobbyName string.
2.5. Test you App!
You can see (null) where no hobbies of the user. So you can create a Predicate to check, if user has hobby we list, and in other case no.
3. SortDescriptor and NSPredicate
First of all do look at this SortDescriptor code, we use it, and then we can see our list just in alphabetical.
3.1. Put in your AppDelegate.m file in the (NSArray*)getAllUserRecords code, after the NSEntityDescriptions and before the NSError line this 2 line:
3.2. Run your simulator, and you can see, that your list ordered alphabetical. You can test with one user which start with “a”, and one user which start with “z”.
3.3. Now we can use NSPredicate to check, that user has hobby or not. And if not, we don’t list them.
In this case, you see in your simulator just those users, who have no hobby, exactly have a hobby “null” value. You created these users, at the 1.4., when we have no hobby entity.
If you change the count value from 1 to 0: (hobbiesofuser.@count != 0), you will see those users, who has at least 1 hobby. Maybe you leave blank the TextField, in this case the hobby value will be @“”, but not “(null)”. So the count of the hobbiesofuser relationship will be 1, and your user will be list. So when you use 0 in Predicate, you would like to not show those, who has 0 hobby. I did some test without enter hobby in textField3. My result:
3.4. You can use multiple conditions using “AND” between conditions, and you can use NSPredicate to filter your array.
For example, we would like to see just those users, who’s name hasn’t contain “user” string, we can use this predicate code in your MTableViewcontroller.m file, in cellForRowAtindexpath before this line: User *user1=[self.myArray objectAtIndex:indexPath.row];
And the result: list without username, which contain “user”.
NEXT
Download the Xcode template from Github
The next part of the series:Â
Core Data tutorial part 4.2 / 5 - Using NSFetchedResultsController, Reorder/Moving Table View Cells with Long Press Gesture, and update immediately Core Data displayOrder attribute
If you would like to be notified of my new solutions, pleaseÂ
Scroll Programmatically with keyframe animation Xcode 6.1 IOS 8.1 tutorial
You need a ScrollView in a singleView, or you need a TableView, which has a ScrollView. I use TableViewController, but if you don't use this, please check the UIScrollViewDelegate method before call scrollViewDidScroll: .
3 steps need
Create TableViewControllers - Settings
Detect when the user scrollingÂ
Detect when user stops scrolling, and start our keyframe animation
1. Create TableViewControllers - Settings
1.1. Create TableViewController subclass - New File - Cocoa Touch - UITableViewController type - Name: MyTableviewController
 (adsbygoogle = window.adsbygoogle || []).push({});
 1.2. In MyTableViewController.m file create myArray NSArray, and synthesize it in .m file. Set TableView Section count 1 and rows set to myArray.count
 1.3. Create MyTableViewController on Main storyboard, and add class MyTableViewController.
 1.4. Add UIButton to your ViewController, and ctrl drag to TableViewController, and select “Show” to this segue.
 1.5. Add “Cell” to the Prototype Cell Reuse Identifier
 1.6. Set cell label with this code:
  2. Detect when the user scrollingÂ
 2.1. Detect scroll offset if the user scrolling, use scrollViewDidScroll:
2.2. Test your app, run in Simulator. Now you can check offset data in log.
 3. Detect when user stops scrolling, and start our keyframe animation
We can detect, if the user stop scrolling, just we need to use scrollViewDidEndDragging:. And we can continue the user scrolling with keyframe animation. We can also add if statement, so, if the user scroll more, than “-100” and endDragging, we can start our animation. So we need a startOffset, it will be that value, where the user finishes scrolling. With keyframe animation, you can use more little animation as a one animation, so you can add start time and duration for all steps, and finally, should be compared with the total animation time. I do every step with -50 at startOffset.y, in 0.05 min, so it will be nature scroll animation to your eyes.
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
âś“ Live Streamingâś“ Interactive Chatâś“ Private Showsâś“ HD Quality
Anya is LIVE right now
FREE
Free to watch • No registration required • HD streaming
Core Data tutorial Part 3 / 5 - Edit, Delete Objects
Before you start, here is the first and second part of this series:Â
Core Data tutorial Part 1 / 5 - Create Core Data Managed Object Model with Attributes, Relationships and Delete Rules
Youtube Video Part 1
Core Data tutorial Part 2 / 5 - Save Entity’s Attributes, Relationships, and make FetchRequest
Youtube video for part 2
Check my article: “You must to know! Collection - Core Data simple explanations”Â
IOS 8.1 Xcode 6.1
Steps in Part 3:
1. Edit user1.userName or type1.typeName attributes
2. Test Your App, run Simulator, edit and save your user attributes
3. Delete objects
4. Test your App again, you can now edit user data, and swipe and delete.
1. Edit user1.userName or type1.typeName attributes
1.1. in MyTableViewController.h file import “User.h”, create User *selecteduser property, and then synthesize it in .m file
1.2. do the same in the ViewController.h and .m file
1.3. Create UnwindSegue code in ViewController.m file:
1.4. ctrl drag&drop the PrototypeCell on the storyboard to the MyTableViewController Exit icon, and choose “unwindFromSegue:”, and then add Segue name: “selectuser”.
1.5. in MyTableViewController.m file #import “ViewController.h” and put the didSelectRowAtIndexPath:code:
3.1. in your MYTableViewController.m file in green text find and activate canEditRowAtIndexPath: and commitEditingStyle: codes
3.2. change the Editing StyleDelete if statement part code tot this, because you need to delete object from database too, not just delete the row from the screen:
4. Test your App again, you can now edit user data, and swipe and delete.
NEXT
Download the Xcode template from Github
The next part of the series: Core Data part 4 / 5 - Change Core Data model, using SortDescriptor, NSPredicate FetchRequest and filter Array using Predicate.
Youtube video Part 4
If you would like to be notified of my new solutions, pleaseÂ
Core Data tutorial Part 2 / 5 - Save Entity’s Attributes, Relationships, and make FetchRequest
IOS 8.1, Xcode 6.1
Before you start, here is the first part of this series:Â
Core Data tutorial Part 1 / 5 - Create Core Data Managed Object Model with Attributes, Relationships and Delete Rules
Youtube Video Part 1
Maybe you should check my article: “You must to know! Collection - Core Data simple explanations”Â
Our goal to save User Entity’s Name (userName-string) and the type of the user, so the Type Entity’s Name (typeName-string), and FetchRequest in UITableViewController the user name and the type Relationship’s Name attribute.
Looks like this:
Steps:
ViewController settings - UITextFields, UIButton, TableViewController, Show SegueÂ
Set property NSManagedObjectModel, NSManagedObjectContext and NSPersistenStoreCoordinator
Save User Entity’s data and Relationships in ViewController.m file
FetchRequest in TableViewController.m - List users with Type relationship’s attribute
Test your App
Youtube video for part 2
1. ViewController settings - UITextFields, UIButton, TableViewController, Show SegueÂ
1.1. First create 2 TextField, 1 Button on your View, and create IBOutlet in your .h file, and implement <UITextFieldDelegate>…
then click on one of the UITextFields, and in Connection inspector ctrl drag “delegate” and connect to the ViewController (yellow icon). Do the same with the other one.Â
1.2. synthesize the IBOutlets in your ViewController.m file, and put the UITextField codes codes:
// If you touch outside of TextField your editing will stop and keyboard dismiss
1.3. Create a New TableViewController, and create MyTableViewController subclass
1.3.1. Drag&drop the TableViewController from Object library, use Simulated Metrics size refer to one of iPhone sizes:
1.3.2. Create MyTableViewController.h and .m file, so right click - new File - Cocoa Touch class - Type: UITableViewController - Name: MyTableViewController
1.3.3. AddMyTableViewController subclass to your new TableViewController on your storyboard…
… you can use same name as Storyboard ID …
….click on PrototypeCell and set cell Reuse Identifier as “Cell”…
… and create Show segue from myButton to MyTableViewController, and set Identifier: “ListUsersSegue”
3. Save User Entity’s data in ViewController.m file
3.1. in ViewController.m file import MyTableViewController.h, User.h and Type.h files
3.2. put prepareForSegue: code to your ViewController.m. So we save attributes before the TableViewcontroller will appear.
3.3. in prepareForSegue set user attributes to save
4. FetchRequest in TableViewController.m - List users with type relationship attribute
4.1. Core Data FetchRequest, NSPredicate, NSSortDescriptor - “You must to know!”
Check my article: “You must to know! Collection - Core Data simple explanations” 5. part
4.2. Create NSArray FetchRequest in your AppDelegate.m file before @end to request all User objects:
call it in you AppDelegate.h file:
4.3. Create an Array in your MyTableViewController.h file, and then synthesize in .m file and in viewDidLoad call the AppDelegate AllUser fetch request.
So your myArray actually your users group. Now your ViewController.h and m file looks like this:
4.4. Set TableView details, soÂ
- numberOfSectionsInTableView:
- numberOfRowsInSection:
- cellForRowAtIndexPath:Â
4.5. Debug Core Data - Put this code into Product - Scheme - Edit Scheme menu: Run
-com.apple.CoreData.SQLDebug 1
Now, you will see the save and fetch process log.
5.Test your App
Maybe you can set in viewDidLoad this:
self.TextField1.placeholder = @”I am userName”;
self.TextField2.placeholder = @”I am typeName”;
And the result:
If you would like to be notified of my new solutions, please subscribe to my youtube channel and/ or  follow me on twitter!
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
âś“ Live Streamingâś“ Interactive Chatâś“ Private Showsâś“ HD Quality
Anya is LIVE right now
FREE
Free to watch • No registration required • HD streaming
Core Data tutorial Part 1 / 5 - Create Core Data Managed Object Model with Attributes, Relationships and Delete Rules
1. Create SingleView Project using Core Data, create Core Data Model, Entities
1.1. Create New Project, choose SingleView app, and select “Use Core Data” checkbox.
1.2. Create Core Data model with 2 Entity: User and Type and their attributes
Click on model file …
and  then “Add Entity” button …
set the Entity Name as User …
add Name (string), BirthDate (date), and Phone (Float) attributes.
Create the same with Type Entity with typeName (string) attribute
2. Create Entity relationships and delete rulesÂ
2.1. Create Relationship
In our model a user has only one type, but one type can contain multiple users. For example, the family type may contain the brother “Joe” as a user, and “Mom” and “Dad” as well. But Dad can not be in the type of “classmates”, if it’s already in the family type. So set the relationship “one to many” type, and we call “usersoftype”.
At Type Entity add Relationship: “usersoftype”. Destination: “User”. Set the Type: “To Many”, and Delete Rule: “Cascade”. In this case if you delete this type (family), you automatically delete all users which belongs to this type (brother, mother, father).
Create the user’s relationship, which will be “To One”, because the user can belong to only one type. So we call the Relationship: “typeofuser”, Destination: “Type”, Type: “To One”, Delete Rule: “Nullify”, Type: “To One”.
Now you can set the inverse to the Relationship also, that will be: “usersoftype”.
The inverse will be set at Type automatically. You can check it.
2.2. Core Data Relationships - “You must to know!”
Check my article: “You must to know! Collection - Core Data simple explanations” 3. part
2.3. Core Data Delete Rules - “You must to know!”
Check my article: “You must to know! Collection - Core Data simple explanations” 4. part
2.4. Create NSManagedObject subclass
Click in Editor menu to the “Create NSManagedObject subclass…. menu, …
Select the Type and User Entity too!
the result:
then create a New Group called “CoreData”, and drag&drop the core data files there.
Finally:
2.5. Core Data Managed Object, Managed Object Model, and ManagedObjetcContext - “You must to know!”
Check my article: “You must to know! Collection - Core Data simple explanations” 1. part
“You must to know!” Collection - Core Data simple explanations
With Core Data framework you can managed your objects in your App. You can manage your object’s life cycle and object graph, so the attributes and relationships of your entities. You can edit, save modify and rebuild your database. You can group, filtering, organizing your data. I try to explain just the basics with my example.
I’m going to create a model with users who are listed some types (family, classmates, friends, love) and they have various hobbies.
In our model a user can belongs to only one type, but one type can contain multiple users. For example, the family type may contain brother, mother, and father as well. But Father user cannot be in the type of “classmates”, if it belongs to already “family” type.
1. ManagedObject, ManagedObjectModel and ManagedObjectContext
First you need to describe your objects or entities, so you have to create a schema, which can tell what is the property or name or class, and which attributes can you use to describe your object, which belongs to an array. This schema will be the Managed Object Model. For example:
Entity Description:
Name: “User”
Classname: “User”
Property: array
The array property consists collection of attributes and relationships. Example:
Attribute    —————  collection
Name: “userName”
Type: “string”
ValueClass: “NSString”
Attribute    ————— collection
Name: “userBirthDate
Type: “date”
ValueClass: “NSDate”
Relationship  —————  collection
Name: “typeofuser”
Destination Entity: Type
More info about Managed Object Model in Developer LibraryÂ
The ManagedObjectContext is the place or space, her you can manage your collections of managed objects. You can do any modification with your objects and relationships in the same context, so you can manage and follow their life cycle.Â
More info about ManagedObjectContext in Developer Library
2. PersistentStoreCoordinator
Developer Library gives you a very clear explanation:
"A persistent store coordinator associates persistent object stores and a managed object model, and presents a facade to managed object contexts such that a group of persistent stores appears as a single aggregate store. A persistent store coordinator is an instance of NSPersistentStoreCoordinator. It has a reference to a managed object model that describes the entities in the store or stores it manages.
The coordinator is the central object in a Core Data stack. In many applications you just have a single store, but in complex applications there may be several, each potentially containing different entities. The persistent store coordinator’s role is to manage these stores and present to its managed object contexts the facade of a single unified store. When you fetch records, Core Data retrieves results from all of them, unless you specify which store you’re interested in.”
In our example, we have one type with more users, so this will be a one to many, so “to-many” relationship. We can call this relationship “usersoftype”, which located at Type Entity, but the Destination is User Entity.
It has an Inverse, because we have more users with one type, so this relationship from this side, will be “to-one”. And we can call “typeofuser” which belongs to User Entity, but the Destination will be “Type”, because the connection end/belongs at/to “Type” Entity.
You can see in Xcode the Core Data model in Graph Editor Style, and you can check yourself:
"You can specify a relationship as being to-one or to-many. To-one relationships are represented by a reference to the destination object. To-many relationships are represented by mutable sets (although fetched properties are represented by arrays). Implicitly, “to-one” and “to-many” typically refer to “one-to-one” and “one-to-many” relationships respectively. A many-to-many relationship is one where a relationship and its inverse are both to-many. These present some additional considerations, and are discussed in greater detail in Many-to-Many Relationships.”
Source Core Data Relationships
4. Delete Rules
We have Type Entity and User Entity, and we have usersoftype and typeofuser relationships. When you do the settings, this will look like this:
User Entity
Relationship: “typeofuser”
Destination: “Type”
Type: “To One”
Delete Rule: “Nullify”
In this case we just tell to the destination object “null”, when the object deleted.
Type Entity
Relationship “usersoftype”
Destination: “User”
Type: “To Many”
Delete Rule: “Cascade”
In this case if you delete this type (family), you automatically delete all users, which belongs to this type (brother, mother, father).
"Relationship Delete Rules
A relationship’s delete rule specifies what should happen if an attempt is made to delete the source object. Note the phrasing in the previous sentence—”if an attempt is made…”. If a relationship’s delete rule is set to Deny, it is possible that the source object will not be deleted. Consider again a department’s employees relationship, and the effect that the different delete rules have.
Deny
If there is at least one object at the relationship destination, then the source object cannot be deleted.
For example, if you want to remove a department, you must ensure that all the employees in that department are first transferred elsewhere (or fired!) otherwise the department cannot be deleted.
Nullify
Set the inverse relationship for objects at the destination to null.
For example, if you delete a department, set the department for all the current members to null. This only makes sense if the department relationship for an employee is optional, or if you ensure that you set a new department for each of the employees before the next save operation.
Cascade
Delete the objects at the destination of the relationship.
For example, if you delete a department, fire all the employees in that department at the same time.
No Action
Do nothing to the object at the destination of the relationship.
For example, if you delete a department, leave all the employees as they are, even if they still believe they belong to that department.”
4. Fetch Request, NSPredicate and NSSortDescriptor
If you would like to edit, or show your objects or your object’s attribute, you should do a fetch request. You can call your Entity’s objects in an Array, and then you can work with them. You can use NSPredicate to tell in which parameters would you like to fetch your objects, and of course you can tell for example, that you want to order your results ascending alphabetical. You can do this with NSSortDescriptor.
"A fetch request tells a managed object context the entity of the managed objects that you want to fetch; optionally, it specifies other aspects such as constraints on the values the objects’ properties must have and the order you want the objects returned in. A fetch request is an instance of NSFetchRequest. The entity it specifies is represented by an instance of NSEntityDescription; any constraints are represented by an NSPredicate object, and the ordering by an array of one or more instances of NSSortDescriptor. These are akin to the table name, WHERE clause, and ORDER BY clauses of a database SELECT statement respectively.
You execute a fetch request by sending a message to a managed object context. The context returns an array containing the objects (if any) that matched the request.”
Fetch request source
5. Now you can start the Core Data tutorial
I create a serie, you can learn everything in these parts!Â
You can find the first part here:Â
Core Data tutorial Part 1 / 5 - Create Core Data Managed Object Model with Attributes, Relationships and Delete Rules
Core Data tutorial Part 2 / 5 - Save Entity’s Attributes, Relationships, and make FetchRequest
If you would like to be notified of my new solutions, please subscribe to my youtube channel and/ or  follow me on twitter!