• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:20:00 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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 =@"?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:@"OST"];
        [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)connectionNSURLConnection *)connection didReceiveResponseNSURLResponse *)response{
 [myNSMDataFromPOSTServer setLength:0];
}
- (void)connectionNSURLConnection *)connection didReceiveDataNSData *)data{
[myNSMDataFromPOSTServer appendData:data];
}
- (void)connectionDidFinishLoadingNSURLConnection *)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)parserNSXMLParser *)parser foundCharactersNSString *)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)joinbtnClickid)sender
{
    [self serverconnectionPost];
}   

注意 - ServerString 包含 Web 服务的 url。

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

如何将核心数据同步到网络服务。有人做过吗?提前致谢!



Best Answer-推荐答案


您可以使用 AFNetworking 框架来处理所有与网络相关的操作,无论是解析 JSON 数据还是 XML 数据。要解析 XML 数据,您可以看到 this回答。

有关 AFNetworking 的更多信息,请参阅 this .

编辑: 关注 this教程以了解有关将核心数据发布到服务器的想法。

关于ios - 如何在 objective-c 中将核心数据对象同步到 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41163456/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap