- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在带有 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/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!