Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Windows Azure > Windows Azure (Mobile Services) and iOS (iPhone , iPad) > ตอนที่ 5 : iOS / iPhone สร้างตาราง Table บน Mobile Services และการ Insert ข้อมูล



Clound SSD Virtual Server

ตอนที่ 5 : iOS / iPhone สร้างตาราง Table บน Mobile Services และการ Insert ข้อมูล

ตอนที่ 5 : iOS / iPhone สร้างตาราง Table บน Mobile Services และการ Insert ข้อมูล บทความนี้ขอต่อจากตอนที่แล้ว ซึ่งจะเป็นการสร้าง Table ไว้สำหรับจัดเก็บข้อมูลบน Mobile Services และ การเขียน iOS App เพื่อ สร้าง Column หรือฟิวด์ พร้อม ๆ กับการส่งข้อมูลจาก iOS แล้วนำไปจัดเก็บ Insert ไว้ใน Table ของ Mobile Services บน Windows Azure

iOS/iPhone Mobile Services Create Table

ตอนนี้เรามี Mobile Services อยู่ 1 ตัว

iOS/iPhone Mobile Services Create Table

เลือก iOS

iOS/iPhone Mobile Services Create Table

ในหน้า Get Started จะแสดงรายละเอียดของการเชื่อมต่อ และได้อธิบายไว้ในบทความก่อน ๆ หน้านี้แล้ว ในขั้นตอนนี้ยังไม่ต้องทำอะไร

ขั้นตอนการสร้าง Table หรือตาราง

iOS/iPhone Mobile Services Create Table

คลิกที่ DATA และเลือก CREATE

iOS/iPhone Mobile Services Create Table

ใส่ชื่อตารางในที่นี้จะใส่เป็น MyMember

iOS/iPhone Mobile Services Create Table

ได้ตารางขึ้นมา 1 รายการชื่อว่า MyMember








iOS/iPhone Mobile Services Create Table

ให้คลิกเข้าไปใน Table (ตาราง) ซึ่งตอนนี้ยังไม่มี Column และ Rows

iOS/iPhone Mobile Services Create Table

เมนู SCRIPT เป็นพวก Script ที่ไว้ทำหน้าที่รับข้อมูลจาก iOS แล้ว Insert ลงใน Table การทำงานคล้าย ๆ กับ Stored Procedure ซึ่งเราสามารถเขียน Script เพิ่มเติมได้ แต่ตอนนี้แนะนำให้กำหนดเป็นค่า Default ซะก่อน

iOS/iPhone Mobile Services Create Table

หลัก ๆ จะมีอยู่ 4 ตัวคือ Insert , Update , Delete , Read

กลับมาบน Project ของ iOS บน Xcode

iOS/iPhone Mobile Services Create Table

ตอนนี้ Project ของเรายังไม่มีอะไร เป็นเพียงโครงสร้างเปล่า ๆ ที่ประกอบด้วย xib, .h และ .m จำนวน 1 ชุด และมีการเรียกใช้ Framework ของ WindowsAzureMobileServices.framework (วิธี Import ดูได้จากบทความก่อนหน้านี้)

ในการเขียน iOS ติดต่อกับ Table ที่อยู่บน Mobile Services เราจะสร้าง Class ไว้สำหรับการจัดการกับ Table นั้น ๆ โดยให้เราสร้าง Class ที่เป็น .h และ .m ขึ้นมา 1 ชุด (ไม่ต้องใช้ .xib)

iOS/iPhone Mobile Services Create Table

คลิกขวาที่ Project -> New File...

iOS/iPhone Mobile Services Create Table

เลือก Objective-C Class

iOS/iPhone Mobile Services Create Table

เนื่องจาก Table ใน Mobile Services ชื่อว่า MyMember เลยตั้งชื่อว่า MyMemberService และเลือก Subclass เป็น NSObject (สามารถตั้งเป็นชื่ออะไรก็ได้)

iOS/iPhone Mobile Services Create Table

ได้ Class ไฟล์ชื่อ MyMemberService.h และ MyMemberService.m ขึ้นมา 1 ชุด และมีคำสั่งต่าง ๆ ดังนี้

MyMemberService.h
//
//  MyMemberService.h
//  myAppMobileService
//
//  Created by Weerachai on 7/21/56 BE.
//  Copyright (c) 2556 Weerachai. All rights reserved.
//

#import <WindowsAzureMobileServices/WindowsAzureMobileServices.h>
#import <Foundation/Foundation.h>


typedef void (^QSCompletionBlock) ();
typedef void (^QSBusyUpdateBlock) (BOOL busy);


@interface MyMemberService : NSObject

@property (nonatomic, strong)   NSArray *items;
@property (nonatomic, strong)   MSClient *client;
@property (nonatomic, copy)     QSBusyUpdateBlock busyUpdate;


+ (MyMemberService *)defaultService;


- (void)addItem:(NSDictionary *)item
     completion:(QSCompletionBlock)completion;

- (void)handleRequest:(NSURLRequest *)request
                 next:(MSFilterNextBlock)next
             response:(MSFilterResponseBlock)response;

@end


MyMemberService.m
//
//  MyMemberService.m
//  myAppMobileService
//
//  Created by Weerachai on 7/21/56 BE.
//  Copyright (c) 2556 Weerachai. All rights reserved.
//

#import "MyMemberService.h"
#import <WindowsAzureMobileServices/WindowsAzureMobileServices.h>


@interface MyMemberService() <MSFilter>

@property (nonatomic, strong)   MSTable *table;
@property (nonatomic)           NSInteger busyCount;

@end

@implementation MyMemberService

@synthesize items;


+ (MyMemberService *)defaultService
{
    // Create a singleton instance of MyMemberService
    static MyMemberService* service;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        service = [[MyMemberService alloc] init];
    });
    
    return service;
}

-(MyMemberService *)init
{
    self = [super init];
    
    if (self)
    {
        // Initialize the Mobile Service client with your URL and key
        MSClient *client = [MSClient clientWithApplicationURLString:@"https://thaicreate.azure-mobile.net/"
        applicationKey:@"uJPAhkAkcTBuTyCNxaOSnDKFzkoYqB49"];
        
        // Add a Mobile Service filter to enable the busy indicator
        self.client = [client clientWithFilter:self];
        
        // Create an MSTable instance to allow us to work with the TodoItem table
        self.table = [_client tableWithName:@"MyMember"];
        
        self.items = [[NSMutableArray alloc] init];
        self.busyCount = 0;
    }
    
    return self;
}



-(void)addItem:(NSDictionary *)item completion:(QSCompletionBlock)completion
{
    // Insert the item into the TodoItem table and add to the items array on completion
    [self.table insert:item completion:^(NSDictionary *result, NSError *error)
     {
         [self logErrorIfNotNil:error];
         
         NSUInteger index = [items count];
         [(NSMutableArray *)items insertObject:result atIndex:index];
         
         // Let the caller know that we finished
         completion();
     }];
}

- (void)busy:(BOOL)busy
{
    // assumes always executes on UI thread
    if (busy)
    {
        if (self.busyCount == 0 && self.busyUpdate != nil)
        {
            self.busyUpdate(YES);
        }
        self.busyCount ++;
    }
    else
    {
        if (self.busyCount == 1 && self.busyUpdate != nil)
        {
            self.busyUpdate(FALSE);
        }
        self.busyCount--;
    }
}

- (void)logErrorIfNotNil:(NSError *) error
{
    if (error)
    {
        NSLog(@"ERROR %@", error);
    }
}


- (void)handleRequest:(NSURLRequest *)request
                 next:(MSFilterNextBlock)next
             response:(MSFilterResponseBlock)response
{
    // A wrapped response block that decrements the busy counter
    MSFilterResponseBlock wrappedResponse = ^(NSHTTPURLResponse *innerResponse, NSData *data, NSError *error)
    {
        [self busy:NO];
        response(innerResponse, data, error);
    };
    
    // Increment the busy counter before sending the request
    [self busy:YES];
    next(request, wrappedResponse);
}

@end

Class เหล่านี้ผมทำการ Apply มาจากตัวอย่าง Demo ของ Windows Azure ฉะนั้น มันสามารถทำงานได้เป็นอย่างดี และโครงสร้างนี้เป็น มาตรฐาน สามารถนำไป Apply ได้กับการเขียนในรูปแบบต่าง ๆ โดยเราแค่เขียน function / method เพิ่มตามที่ต้องการเท่านั้น

ประเด็นสำคัญจะอยู่ที่

-(MyMemberService *)init
{
    self = [super init];
    
    if (self)
    {
        // Initialize the Mobile Service client with your URL and key
        MSClient *client = [MSClient clientWithApplicationURLString:@"https://thaicreate.azure-mobile.net/"
        applicationKey:@"uJPAhkAkcTBuTyCNxaOSnDKFzkoYqB49"];
        
        // Add a Mobile Service filter to enable the busy indicator
        self.client = [client clientWithFilter:self];
        
        // Create an MSTable instance to allow us to work with the TodoItem table
        self.table = [_client tableWithName:@"MyMember"];
        
        self.items = [[NSMutableArray alloc] init];
        self.busyCount = 0;
    }
    
    return self;
}

อันนี้เป็นการกำหนด App URL และ App Key ที่อยู่ใน Mobile Services ของเรา และที่สำคัญคือชื่อ Table

-(void)addItem:(NSDictionary *)item completion:(QSCompletionBlock)completion
{
    // Insert the item into the TodoItem table and add to the items array on completion
    [self.table insert:item completion:^(NSDictionary *result, NSError *error)
     {
         [self logErrorIfNotNil:error];
         
         NSUInteger index = [items count];
         [(NSMutableArray *)items insertObject:result atIndex:index];
         
         // Let the caller know that we finished
         completion();
     }];
}

เป็น method สำหรับการ Insert ข้อมูล

iOS/iPhone Mobile Services Create Table

กลับมายังหน้า View ของ App ให้ทำการสร้าง Label ดังรูป เพื่อไว้แสดงข้อความเมื่อทำการ Insert ข้อมูลเรียบร้อย โดยทำการเชื่อม IBOutlet

ให้เรียบร้อยด้วย จากนั้นในไฟล์ ViewController.h และ ViewController.m ขียนคำสั่งดังนี้

ViewController.h
//
//  ViewController.h
//  myAppMobileService
//
//  Created by Weerachai on 5/12/56 BE.
//  Copyright (c) 2556 Weerachai. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UILabel *lblStatus;
}

@end


ViewController.m
//
//  ViewController.m
//  myAppMobileService
//
//  Created by Weerachai on 5/12/56 BE.
//  Copyright (c) 2556 Weerachai. All rights reserved.
//

#import <WindowsAzureMobileServices/WindowsAzureMobileServices.h>
#import "MyMemberService.h"

#import "ViewController.h"

@interface ViewController ()

// Private properties
@property (strong, nonatomic)   MyMemberService   *memberService;

@end


@implementation ViewController

@synthesize memberService;


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Start Service
    self.memberService = [MyMemberService defaultService];
    
    // Add Data Table Service
    NSDictionary *item = @{ @"name":@"Win", @"email":@"[email protected]" };
    [self.memberService addItem:item completion:^
     {
         lblStatus.text = @"Insert Data Successfully.";
     }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [lblStatus release];
    [super dealloc];
}
@end


เป็นการเรียกใช้งาน Class สำหรับการ Insert ข้อมูลไปไว้บน Mobile Services ของ Windows Azure โดยประเด็นสำคัญอยู่ที่

// Start Service self.memberService = [MyMemberService defaultService]; // Add Data Table Service NSDictionary *item = @{ @"name":@"Win", @"email":@"[email protected]" }; [self.memberService addItem:item completion:^ { lblStatus.text = @"Insert Data Successfully."; }];

คำสั่งนี้จะเป็นการ Insert ข้อมูลไปจัดเก็บไว้ที่ Table ของ Mobile Services และเราจะเห็นว่า ตั้งแต่เราสร้าง Table เรายังไม่มีการสร้าง Column หรือฟิวด์เลย เพราะเราไม่จำเป็ฯจะต้องสร้าง เพราะเมื่อมีการ Insert ข้อมูลเข้าไปใหม่ ถ้าฟิวด์ไหนไม่มี จะมีการสร้างขึ้นใหม่ให้

อัตโนมัติ และในตัวอย่างนี้เรามีอยู่ 2 ฟิวด์คือ name และ email

iOS/iPhone Mobile Services Create Table

ทดสอบการรัน App

iOS/iPhone Mobile Services Create Table

เมื่อกลับไปดูที่ Data บน Mobile Services ข้อมูลจะถูก Insert เข้า ซึ่งเราจะเห็นว่ามี Column ชื่อ id ด้วย เพราะ Column นี้จำเป็นที่ระบบจะสร้างให้อัตโนัติ และเราสามารถเรียกมันใช้งานได้เช่นกัน

ดาวน์โหลดได้จากข้างล่างของบทความ








บทความถัดไปที่แนะนำให้อ่าน


บทความที่เกี่ยวข้อง


   
Share

Property & Method (Others Related)

How to read data in Mobile Services - iOS / iPhone (Windows Azure)
How to insert data to Mobile Services - iOS / iPhone (Windows Azure)
How to update data to Mobile Services - iOS / iPhone (Windows Azure)
How to delete data in Mobile Services - iOS / iPhone (Windows Azure)

ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-06-15 19:50:17 / 2017-03-24 11:45:32
  Download : Download  ตอนที่ 5 : iOS / iPhone สร้างตาราง Table บน Mobile Services และการ Insert ข้อมูล
 Sponsored Links / Related

 
ตอนที่ 1 : รู้จัก iOS กับ Mobile Services บน Windows Azure คืออะไร
Rating :

 
ตอนที่ 2 : เตรียมความพร้อมของ iOS / iPhone ก่อนที่จะเขียนบน Mobile Services
Rating :

 
ตอนที่ 3 : การสร้าง iOS / iPhone กับ Mobile Services และการเรียกใช้งานแบบง่าย ๆ
Rating :

 
ตอนที่ 4 : สร้าง Project iOS / iPhone และการเชื่อมต่อกับ Library ของ Mobile Services
Rating :

 
ตอนที่ 6 : iOS / iPhone อ่าน Data จาก Table ของ Mobile Service และแสดงผลบน App
Rating :

 
ตอนที่ 7 : อ่านข้อมูล Mobile Services แบบมีเงื่อนไข Where และแสดงผล iOS / iPhone
Rating :

 
ตอนที่ 8 : การทำ Authentication in Azure Mobile Services ด้วย iOS / iPhone
Rating :

 
ตอนที่ 9 : การทำ Push Notifications in Mobile Services ด้วย iOS / iPhone
Rating :

 
ตอนที่ 10 : การทำ Validate และ Modify data in Mobile Services บน iOS / iPhone
Rating :

 
ตอนที่ 11 : การทำ Refine Mobile Services queries with paging บน iOS / iPhone
Rating :

 
ตอนที่ 12 : การเขียน Scripts to authorize users in Mobile Services บน iOS / iPhone
Rating :

 
ตอนที่ 13 : Show Case 1 : Register Form (iOS / iPhone and Mobile Services)
Rating :

 
ตอนที่ 14 : Show Case 2 : Login User Password (iOS / iPhone and Mobile Services)
Rating :

 
ตอนที่ 15 : Show Case 3 : Update Data (iOS / iPhone and Mobile Services)
Rating :

 
ตอนที่ 16 : Show Case 4 : Delete Data (iOS / iPhone and Mobile Services)
Rating :

 
ตอนที่ 17 : บทความอื่น ๆ เกี่ยวกับ iOS / iPhone Mobile Services บน Windows Azure
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่