菜鸟教程小白 发表于 2022-12-11 18:20:00

ios - 如何在 objective-c 中将核心数据对象同步到 Web 服务


                                            <p><p>我是 iOS 新手,在将核心数据与 Web 服务同步时遇到了问题。我的网络服务是这样的</p>

<pre><code>POST /webservice.asmx HTTP/1.1
Host: Url
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: &#34;http://tempuri.org/Method&#34;

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

<p>这就是我在核心数据中保存数据的方式</p>

<pre><code>   NSManagedObjectContext *context = ;

                if (self.device) {
                  // Update existing device
                  ;
                  ;
                  ;
                  ;
                  ;
                  ;
                  ;
                  ;
                  ;

                } else {
                  // Create a new device
                  NSManagedObject *newDevice = ;
                  ;
                  ;
                  ;
                  ;
                  ;
                  ;
                  ;
                  ;
                  ;
                }



                NSError *error = nil;
                // Save the object to persistent store
                if (!) {
                  NSLog(@&#34;Can&#39;t Save! %@ %@&#34;, error, );
                }

NSManagedObjectContext *managedObjectContext = ;
            NSFetchRequest *fetchRequest = [ initWithEntityName:@&#34;Entity Name&#34;];
            self.devices = [ mutableCopy];
</code></pre>

<p>这就是我通常使用代码将数据发布到 Web 服务的方式</p>

<pre><code>    -(void)serverconnectionPost{

      NSString *MethodString =@&#34;?op=Method&#34;;
      NSString *ServerfullString =;

      int auid=0;
      long regionid=;
      long branch=;
      long site=;
      long auditnameid=;
      long checkpoint=;
      long methodofmeasume=;
      long rdbchecklist=;
      long UserId=;

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

      NSData *postData = ;

      NSString *postLength = ];
      NSMutableURLRequest *request = [ init];
      ];
      ;
      ;
      ;
      NSLog(@&#34;URL = %@&#34;,request);
      ;
      myNSUConnectionPOSTObj = [ initWithRequest:request delegate:self];
      NSLog(@&#34;Connection link =%@&#34;,myNSUConnectionPOSTObj);
      if(myNSUConnectionPOSTObj) {
            myNSMDataFromPOSTServer =[ init];
            NSLog(@&#34;Connection Successful&#34;);

      } else {
            NSLog(@&#34;Connection could not be made&#34;);
      }
    }

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

      ;
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
myMutableStringPOSTObj=[initWithString:string];
    NSLog(@&#34;Array String: %@&#34;,myMutableStringPOSTObj);
    NSData *dataPost = ;
    responsePOSTdict = ;
    NSLog(@&#34;JSON DATA = %@&#34;,responsePOSTdict);
}

-(IBAction)joinbtnClick:(id)sender
{
    ;
}   
</code></pre>

<p>注意 - ServerString 包含 Web 服务的 url。</p>

<p>我需要将一个值与网络服务同步,然后将其从核心数据中删除,然后再像同步一样将另一个值删除。</p>

<p>如何将核心数据同步到网络服务。有人做过吗?提前致谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以使用 <code>AFNetworking</code> 框架来处理所有与网络相关的操作,无论是解析 <code>JSON</code> 数据还是 <code>XML</code> 数据。要解析 <code>XML</code> 数据,您可以看到 <a href="https://stackoverflow.com/questions/20402034/parsing-xml-with-afnetworking" rel="noreferrer noopener nofollow">this</a>回答。</p>

<p>有关 AFNetworking 的更多信息,请参阅 <a href="https://github.com/AFNetworking/AFNetworking" rel="noreferrer noopener nofollow">this</a> .</p>

<p><strong>编辑:</strong>
关注 <a href="https://www.raywenderlich.com/15916/how-to-synchronize-core-data-with-a-web-service-part-1" rel="noreferrer noopener nofollow">this</a>教程以了解有关将核心数据发布到服务器的想法。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 objective-c中将核心数据对象同步到 Web 服务,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41163456/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41163456/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 objective-c 中将核心数据对象同步到 Web 服务