2016-12-15 25 views
2

我是iOS新手,我正在面對將核心數據與Web服務同步的問題。我的Web服務是這樣的如何將核心數據對象同步到目標服務中的web服務c

POST /webservice.asmx HTTP/1.1 
Host: Url 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 
SOAPAction: "http://tempuri.org/Method" 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
    <Method xmlns="http://tempuri.org/"> 
     <key1>int</key1> 
     <key2>long</key2> 
     <key3>long</key3> 
     <key4>long</key4> 
     <key5>long</key5> 
     <key6>long</key6> 
     <key7>long</key7> 
     <key8>long</key8> 
     <key9>long</key9> 
    </Method> 
    </soap:Body> 
</soap:Envelope> 

核心數據這怎麼我將數據保存

NSManagedObjectContext *context = [self managedObjectContext]; 

       if (self.device) { 
        // Update existing device 
        [device setValue:Audit forKey:@"Key1"]; 
        [device setValue:MethodID forKey:@"Key2"]; 
        [device setValue:CheckPointID forKey:@"Key3"]; 
        [device setValue:GlobalStringChk forKey:@"Key4"]; 
        [device setValue:RegionID forKey:@"Key5"]; 
        [device setValue:BranchID forKey:@"Key6"]; 
        [device setValue:SiteID forKey:@"Key7"]; 
        [device setValue:AuID forKey:@"Key8"]; 
        [device setValue:userid forKey:@"Key9"]; 

       } else { 
        // Create a new device 
        NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Entity Name" inManagedObjectContext:context]; 
        [newDevice setValue:Audit forKey:@"Key1"]; 
        [newDevice setValue:MethodID forKey:@"Key2"]; 
        [newDevice setValue:CheckPointID forKey:@"Key3"]; 
        [newDevice setValue:GlobalStringChk forKey:@"Key4"]; 
        [newDevice setValue:RegionID forKey:@"Key5"]; 
        [newDevice setValue:BranchID forKey:@"Key6"]; 
        [newDevice setValue:SiteID forKey:@"Key7"]; 
        [newDevice setValue:AuID forKey:@"Key8"]; 
        [newDevice setValue:userid forKey:@"Key9"]; 
       } 



       NSError *error = nil; 
       // Save the object to persistent store 
       if (![context save:&error]) { 
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]); 
       } 

NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; 
      NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Entity Name"]; 
      self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; 

這是怎麼了,我一般使用的代碼將數據發佈到Web服務

-(void)serverconnectionPost{ 

     NSString *MethodString [email protected]"?op=Method"; 
     NSString *ServerfullString =[NSString stringWithFormat:@"%@%@",ServerString,MethodString]; 

     int auid=0; 
     long regionid=[RegionID longLongValue]; 
     long branch=[BranchID longLongValue]; 
     long site=[SiteID longLongValue]; 
     long auditnameid=[AuditNameId longLongValue]; 
     long checkpoint=[CheckPointID longLongValue]; 
     long methodofmeasume=[MethodID longLongValue]; 
     long rdbchecklist=[RDBIDCheckListID longLongValue]; 
     long UserId=[userid longLongValue]; 

     NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>" 
           "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" 
           "<soap:Body>" 
           "<Method xmlns=\"http://tempuri.org/\">" 
           "<Key1>%d</Key1>" 
           "<Key2>%ld</Key2>" 
           "<Key3>%ld</Key3>" 
           "<Key4>%ld</Key4>" 
           "<Key5>%ld</Key5>" 
           "<Key6>%ld</Key6>" 
           "<Key7>%ld</Key7>" 
           "<Key8>%ld</Key8>" 
           "<Key9>%ld</Key9>" 
           "</Method>" 
           "</soap:Body>" 
           "</soap:Envelope>",auid,regionid,branch,site,auditnameid,checkpoint,methodofmeasume,rdbchecklist,UserId]; 

     NSData *postData = [soapMessage dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

     NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[soapMessage length]]; 
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
     [request setURL:[NSURL URLWithString:ServerfullString]]; 
     [request setHTTPMethod:@"POST"]; 
     [request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
     NSLog(@"URL = %@",request); 
     [request setHTTPBody:postData]; 
     myNSUConnectionPOSTObj = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
     NSLog(@"Connection link =%@",myNSUConnectionPOSTObj); 
     if(myNSUConnectionPOSTObj) { 
      myNSMDataFromPOSTServer =[[NSMutableData alloc] init]; 
      NSLog(@"Connection Successful"); 

     } else { 
      NSLog(@"Connection could not be made"); 
     } 
    } 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
[myNSMDataFromPOSTServer setLength:0]; 
} 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 
[myNSMDataFromPOSTServer appendData:data]; 
} 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
NSXMLParser *myNSXMLParserPostObj=[[NSXMLParser alloc]initWithData:myNSMDataFromPOSTServer]; 
     myNSXMLParserPostObj.delegate=self; 
     [myNSXMLParserPostObj parse]; 
     NSLog(@"%@",myNSXMLParserPostObj.parserError); 
     NSString *responseStringWithEncoded = [[NSString alloc] initWithData: myNSMDataFromPOSTServer encoding:NSUTF8StringEncoding]; 
     //NSLog(@"Response from Server : %@", responseStringWithEncoded); 
     NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[responseStringWithEncoded dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil]; 
     serverResponse.attributedText = attrStr; 
     NSString *Str =serverResponse.text; 
     NSLog(@"Server Response =%@",Str); 
     alert3 = [[UIAlertView alloc] initWithTitle:@"Message to display" 
              message:Str 
              delegate:self 
            cancelButtonTitle:@"OK" 
            otherButtonTitles:nil]; 

     [alert3 show]; 
} 
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    myMutableStringPOSTObj=[[NSMutableString alloc]initWithString:string]; 
    NSLog(@"Array String: %@",myMutableStringPOSTObj); 
    NSData *dataPost = [myMutableStringPOSTObj dataUsingEncoding:NSUTF8StringEncoding]; 
    responsePOSTdict = [NSJSONSerialization JSONObjectWithData:dataPost options:0 error:nil]; 
    NSLog(@"JSON DATA = %@",responsePOSTdict); 
} 

-(IBAction)joinbtnClick:(id)sender 
{ 
    [self serverconnectionPost]; 
} 

注 - ServerString包含Web服務的URL。

我需要將一個值與Web服務同步,然後將其從核心數據中刪除,然後像同步一樣刪除另一個值。

如何將核心數據同步到web service.Did任何人都可以做到這一點。提前致謝!

+0

?它非常簡單,將處理所有需要的場景。 –

+0

@VishalSonawane我正在使用XMLParsing和AFNetworking是用於JSON解析。我不確定當我搜索時,我得到AFNNetworking進行JSON解析。 – Muju

+0

請檢查我的答案。 –

回答

0

如果設備來自核心數據,它不能包含NSString。它可以是NSManagedObject或NSDictionary。我想這是NSManagedObject。

在這種情況下,它更適合使用NSManagedObject作爲對象的循環,並分別獲得價值爲九個鍵:你爲什麼不使用AFNetworking框架來發布數據

for (NSManagedObject *object in devices) { 

     NSSString *string1 = [object valueForKey:@"key1"]; 
     NSSString *string2 = [object valueForKey:@"key2"]; 
     NSSString *string3 = [object valueForKey:@"key3"]; 
     NSSString *string4 = [object valueForKey:@"key4"]; 
     NSSString *string5 = [object valueForKey:@"key5"]; 
     NSSString *string6 = [object valueForKey:@"key6"]; 
     NSSString *string7 = [object valueForKey:@"key7"]; 
     NSSString *string8 = [object valueForKey:@"key8"]; 
     NSSString *string9 = [object valueForKey:@"key9"]; 
     // use the strings 

//Hear call your method...It will send value one by one to web service. 
    } 
0

您可以使用AFNetworking框架來處理所有聯網相關操作,分析JSON數據或XML數據。要解析XML的數據,你可以看到this的答案。

有關AFNetworking的更多信息,請參閱this

編輯: 按照this教程,以獲取有關核心數據發佈到服務器的想法。

+0

感謝您的迴應。我能夠發佈數據。我想要代碼或提示如何將核心數據對象同步到Web服務。 – Muju

+0

我已經更新了我的答案,請檢查。 –

相關問題