gpt4 book ai didi

iOS - 通过单例实现连接到服务器

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:15:55 25 4
gpt4 key购买 nike

我在带有 rest APLI 的服务器中有 json 文件,我想与服务器通信,现在我使用此方法连接到服务器并在我的 viewdidload 方法中调用它,我的问题是,最好的方法是什么这个,我可以使用单例吗?如果是,它应该是什么样子?提前致谢!

感谢答案部分中的任何代码建议。

- (void)viewDidLoad
{
[super viewDidLoad];

_mapView.showsUserLocation = YES;
_mapView.delegate = self;
[self fetchData];
}



-(void)fetchData
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:BASED_URL]];
[request setHTTPMethod:@"GET"];
[request setValue:@"/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

NSURLResponse *response;
NSData *GETReply = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[GETReply bytes] length:[GETReply
length] encoding: NSASCIIStringEncoding];


CLLocationCoordinate2D location;
NSMutableArray *newAnnotations = [NSMutableArray array];
NSError *error;
NSArray *array = [NSJSONSerialization JSONObjectWithData:GETReply
options:0
error:&error];
if (error != nil)
{
// handle the error
}

for (NSDictionary *dictionary in array)
{

location.latitude = [dictionary[@"latitude"] doubleValue];
location.longitude = [dictionary[@"longitude"] doubleValue];

// create the annotation
MyAnnotation *newAnnotation;


newAnnotation = [[MyAnnotation alloc] init];
newAnnotation.company = dictionary[@"company"];

newAnnotation.coordinate = location;

[newAnnotations addObject:newAnnotation];
}
[self.mapView addAnnotations:newAnnotations];
}

更新:根据评论,Singleton 不是执行网络功能所必需的,也不建议这样做。

我怎样才能改进这些代码,如果你能给我改进代码的解决方案,我将不胜感激

最佳答案

基于我的评论。

Singleton 用于在整个应用程序中创建单个实例。在网络事件的情况下,不建议使用单例,因为您可能需要一次创建多个网络连接,这是行不通的。您可能会选择静态函数,并选择一些不错的库,如 AFNetworking 来减少您的代码。此外,您应该使用异步网络事件而不是同步,因为如果获取数据需要时间,它会阻止应用程序事件

要修改您的代码,您可以按照以下代码进行操作。

创建一个 ApiManager 类来执行所有网络事件,将所有代码放入类中。见下文

ApiManager.h

导入

@interface ApiManager : NSObject
+(void)fetchCoordinates:(void (^) (id result))success failure:(void (^) (NSError *error))failure;
@end

ApiManager.m

#import "ApiManager.h"

#define BASED_URL @""


@implementation ApiManager
+(void)fetchCoordinates:(void (^) (id result))success failure:(void (^) (NSError *error))failure{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:BASED_URL]];
[request setHTTPMethod:@"GET"];
[request setValue:@"/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

NSURLResponse *response;
NSData *GETReply = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:nil];

NSError *error;
NSArray *array = [NSJSONSerialization JSONObjectWithData:GETReply
options:0
error:&error];

if(!error){
success(array);
}else
failure(error);

}
@end

用法

[ApiManager fetchCoordinates:^(id result) {

NSArray *array=(NSArray*)result;

NSMutableArray *newAnnotations = [NSMutableArray array];

for (NSDictionary *dictionary in array)
{

location.latitude = [dictionary[@"latitude"] doubleValue];
location.longitude = [dictionary[@"longitude"] doubleValue];

// create the annotation
MyAnnotation *newAnnotation;


newAnnotation = [[MyAnnotation alloc] init];
newAnnotation.company = dictionary[@"company"];

newAnnotation.coordinate = location;

[newAnnotations addObject:newAnnotation];
}
[self.mapView addAnnotations:newAnnotations];
} failure:^(NSError *error) {

}];

希望对您有所帮助。

干杯。

关于iOS - 通过单例实现连接到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24883506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com