Wednesday, October 1, 2014

How to overload methods in WCF Service


Solution: How to overload methods in WCF Service:


using System.ServiceModel;
   [ServiceContract]
    public interface IMyService
    {
        [OperationContract(Name="GetData ")]
 /// for accessing the method of wcf service by uri
[WebGet(RequestFormat = WebMessageFormat.Xml, UriTemplate = "GetData")]
        List<product> getData();


        [OperationContract(Name = "GetData with Parameter")]
[WebGet(RequestFormat = WebMessageFormat.Xml, UriTemplate = "GetData /{ID}")]
        product HelloWorld(string name);
    }
}

Thursday, June 26, 2014

iOS Core Data Tutorial

In this tutorial ( iOS Core Data Tutorial ), we will learn how to save and fetch data in iPhone Apps and also manage relationships between them. Will will create a phone book core data project with complete source code provided in the end.

Final Project – iOS Core Data Tutorial

ios core data tutorial - Final Project

What is Core Data in iOS

Core Data is a powerful unified framework Provided by Apple for persisting data in the applications. It is cheap in terms of processes usage, and relationship between data can also be maintained easily.

Tutorial Overview

In this Tutorial , We will learn basic implementation of Core data
  • How to Add core data files to an existing Project.
  • Manage NSManageObject Classes.
  • Use Relationship among NSManageObjects.
  • Fetching data.
  • Showing Data to the user.

Initial Setup

This tutorial aims at core data implementation for beginners, so we have already set up the project with the views we needed and the controllers hierarchy.
For understanding the setup, you need to have basic knowledge of Table view controller and adding new project  (If you don’t know this , you can read our previous tutorialsfrom here  )
Short note on already set up project (Project can be downloaded from link below )
  • MainViewController – Initial View controller :  which contains a form to add new entry to your phone book and a button to show already added entry.
  • PhoneBookTableViewController : This will be used to show already added phone book entries.
You can download the initial project and run it and you will see the form to add entry and tapping show entry button will take you to table view controller (its empty as we have not added any data to it yet)
Now we will move on to adding core data functionality.

Add Missing Framework

  • Tap on project file , go to Targets.
  • Open the summary tab.
  • Click on ‘+’ inside Linked Frameworks and Libraries (shown as 1 in the screen).
  • Add CoreData.Framework.
  • Move this framework under Framework folder inside project Navigator Tab.
ios core data tutorial - adding framework

Adding Missing Files

  • From Xcode,click on the File Tab and go to new file (or click Cmd + N).
  • Select Core data from the iPhone OS group.
  • Select Data Model and click Next.
  • Give the file a name (the project name is a good choice) and click Create.
ios Core Data Tutorial adding Model File

Now we have an Empty file with name “PhoneBook.xcdatamodeld”

Adding Missing Objects

1.  Open PhoneBook-Prefix.pch (Your project pch file ) and add following code to it
1
#import <CoreData/CoreData.h>
This is added to make project compile core data  framework and make it available for all files in the project as it is being added to .pch file (pre-compiled header)
2. Open AppDelegate.h and add following code (objects)
1
2
3
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator ;
As you can see, We have created instance of  NSManagedObjectModel , NSManagedObjectContext , NSPersistentStoreCoordinator respectively , that will be used to deal with data base .
3. Now Open AppDelegate.m and add following code
1
2
3
4
5
6
@implementation ApDelegate
 
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
 
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
4. To the same file (AppDelegate.m) , add Following Methods also :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// 1
- (NSManagedObjectContext *) managedObjectContext {
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator: coordinator];
}
 
return _managedObjectContext;
}
 
//2
- (NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
_managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
 
return _managedObjectModel;
}
 
//3
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
stringByAppendingPathComponent: @"PhoneBook.sqlite"]];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:[self managedObjectModel]];
if(![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:storeUrl options:nil error:&error]) {
/*Error for store creation should be handled in here*/
}
 
return _persistentStoreCoordinator;
}
 
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
1) Method will be returning “managedObjectContext” object associated with out data base file (Sqlite ).
2) Method will be returning “managedObjectModel” object associated with out data base file (Sqlite ).
3) Method will be returning “persistentStoreCoordinator” object associated with out data base file (Sqlite ).

Some useful Definitions

Persistent Store Coordinator:
As the name suggest it is a coordinator , that coordinates between manage object context and low level file saved in our data base (Sqlite file) , you do not have to think much about it as this stage. We are not gonna use it directly, it will only be used while setting NSManageObjectContext . Linking it to our Persistent store coordinator which in turn will be linked with our Sqlite file (PhoneBook.sqlite for our case) .
Managed object context
You can think of a managed object context as an intelligent scratch pad. When you fetch objects from a persistent store, you bring temporary copies onto the scratch pad . You can then modify those objects however you like. Unless you actually save those changes, however, the persistent store remains unaltered.
This will be most important as we will be using it to make queries on our data (fetching , updating , deleting and adding data).
NSManageObject Model 
A managed object model is an instance of the NSManagedObjectModel class. It describes a schema (contains definitions) for objects (also called entities )—that you use in your application.
Properties (Attributes of entity )
An entity’s properties are its attributes and relationships. Amongst other features, each property has a name and a type. A property name cannot be the same as any no-parameter method name of NSObject or NSManagedObject—for example, you cannot give a property the name “description” (see NSPropertyDescription).

Generating Our Model

  1. Tap on “PhoneBook.xcdatamodeld”
  2. You will see a screen like this
ios Core Data Tutorial Generate Model
1) Tap this Button to add New Entity
2) Tap this to Add new Property (Attribute) to selected Entity.
3) Tap this to Add new Relationship to selected Entity.
4) This Column will tell you the Attribute name.
5) This Column will tell you the Attribute Type.

Using Above info , we have created our Entity named “Record” with 3 Attributes as shown in screen :

ios core data tutorial - model Created
1) This Text  (Record) will be used as class name when you use auto generate. (just remember it for now, Auto generate process will be explained below).

Auto Generating NSManageObject class

Go to File tab and tap on new file ( or Cmd + N) when the ”PhoneBook.xcdatamodeld” is still open
  • Select Core data from the iPhone OS group.
  • Select NSManageObjectSubclass and click Next
  • Record.h and Record.m will be created automatically with all the properties defined.
Now run the project , it will run as before. If there is any warning or error. 
You may have missed any step check all the steps again. But still there is no data as we have not yet saved any  Records to our Data base.

Saving Records to Database

So far , we have added core data files , framework and generating our model file .
Now, We are going to the most important and useful part of the tutorial,
We are going to add Our data to Data base.
 Open MainViewController.m
Add following property   (write below code above method definitions)
1
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
Import Required header Files
1
2
#import "Record.h"
#import "AppDelegate.h"
Add following code to viewDidLoad Method
1
2
3
4
//1
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
//2
self.managedObjectContext = appDelegate.managedObjectContext;
1)  Make an instance of AppDelegate .
2)  Referencing manageObjectContext : manageObjectContext of the Controller points to AppDelegate’s manageObjectContext object.
Change addPhoneBookEntry method to following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- (IBAction)addPhoneBookEntry:(id)sender
{
  // Add Entry to PhoneBook Data base and reset all fields
 
//  1
  Record * newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"Record"
                                                    inManagedObjectContext:self.managedObjectContext];
//  2
  newEntry.firstName = self.firstNameTextfield.text;
  newEntry.lastName = self.lastNameTextfield.text;
  newEntry.city = self.cityTextfield.text;
//  3
  NSError *error;
  if (![self.managedObjectContext save:&error]) {
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
  }
//  4
  self.firstNameTextfield.text = @"";
  self.lastNameTextfield.text = @"";
  self.cityTextfield.text = @"";
  self.phoneNumber1.text = @"";
  self.phoneNumber2.text = @"";
//  5
  [self.view endEditing:YES];
}
1) Creates, configures, and returns an instance of the Record class .
2) We add Values to different Attributes of Record class object.
3) Entity value is saved to data base (Without this our data won’t persist in the memory ). As i already told you its like scratch pad unless you save the work. it will be lost when scratch pad is erased.
4) Removing All Text field in Texfields.
5) Dismissing Keyboard.
ios core data tutorial - AddingData
Screen showing an Example Record
Show Records In TableView
1. Open AppDelegate.h and add Following Methods
1
-(NSArray*)getAllPhoneBookRecords;
2 . Now Open  AppDelegate.m and add implementation of the Method added.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-(NSArray*)getAllPhoneBookRecords
{
  // initializing NSFetchRequest
  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
 
  //Setting Entity to be Queried
  NSEntityDescription *entity = [NSEntityDescription entityForName:@"Record"
                                            inManagedObjectContext:self.managedObjectContext];
  [fetchRequest setEntity:entity];
  NSError* error;
 
  // Query on managedObjectContext With Generated fetchRequest
  NSArray *fetchedRecords = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
 
  // Returning Fetched Records
  return fetchedRecords;
}
3.  Now Open PhoneBookTableViewController.m
Firstly Import some header Files
1
2
#import "AppDelegate.h"
#import "Record.h"
Add a Property of type NSArray to save fetched Objects (Records)
1
@property (nonatomic,strong)NSArray* fetchedRecordsArray;
Modify viewDidLoad Method to look like following
1
2
3
4
5
6
7
8
9
- (void)viewDidLoad
{
  [super viewDidLoad];
  AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
 
  // Fetching Records and saving it in "fetchedRecordsArray" object
  self.fetchedRecordsArray = [appDelegate getAllPhoneBookRecords];
  [self.tableView reloadData];
}
Now , we will use this array as a data source to update our table view
Modify UITableViewData source method to incorporate our fetchedRecordsArray to populate the table cells
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return1;
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
  return [self.fetchedRecordsArray count];
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  staticNSString *CellIdentifier = @"PhoneBookCellIdentifier";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  Record * record = [self.fetchedRecordsArray objectAtIndex:indexPath.row];
  cell.textLabel.text = [NSString stringWithFormat:@"%@ %@, %@ ",record.firstName,record.lastName,record.city];
  return cell;
}
Now Run the program again, and tap on show PhoneBook. You will see Records like this
ios core data tutorial - Records Shown