Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 用JSON查询模拟MVC体系结构_Iphone_Ios_Json - Fatal编程技术网

Iphone 用JSON查询模拟MVC体系结构

Iphone 用JSON查询模拟MVC体系结构,iphone,ios,json,Iphone,Ios,Json,在进行JSON查询时,我很难模拟MVC体系结构 下面,我通过创建一个单独的NSObject类来处理查询,以异步方式获取数据 也就是说,在TableViewController中,我不知道应该在下面的查询方法中添加什么 TableViewController.m - (void)viewDidLoad { [super viewDidLoad]; //refactoring with MVC self.aQueue = [[[NSOperationQueue alloc]

在进行JSON查询时,我很难模拟MVC体系结构

下面,我通过创建一个单独的NSObject类来处理查询,以异步方式获取数据

也就是说,在TableViewController中,我不知道应该在下面的查询方法中添加什么

TableViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    //refactoring with MVC
    self.aQueue = [[[NSOperationQueue alloc] init] autorelease];
    self.storeLogos = [NSMutableDictionary dictionary];

    [self queryStoreData];

}
   -(void)queryStoreData
{
    aStoreQuery = [StoreQuery queryForStores:self];
}


-(void)query:(StoreQuery *)query queryResult:(id)object
        {
            [self.aQueue addOperationWithBlock:^{

                //JSONKit? 




            }

        }
StoreQuery.m

@synthesize myConnection, myRequest, storeData;

+(StoreQuery*)queryForStores:(id<StoreQueryDelegate>)aDelegate
{
    StoreQuery *storeQuery = [[[StoreQuery alloc] init] autorelease];
    storeQuery.delegate = aDelegate;
    storeQuery.myRequest = [NSURLRequest requestWithURL:@"URL"];
    storeQuery.myConnection = [NSURLConnection connectionWithRequest:storeQuery.myRequest delegate:storeQuery];
    storeQuery.storeData = [[NSMutableArray data] retain];
    return storeQuery;

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [self.storeData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.storeData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    NSLog(@"Connection Error: %@",[error localizedDescription]);
    if (self.delegate) {
        [self.delegate request:self didFailWithError:error];
    }

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (self.delegate) {
        [_delegate request:self didFinishWithObject:self.storeData];
    }
}


- (void)dealloc
{
    [myRequest release];
    [myConnection release];
    [storeData release];
    [super dealloc];
}
@synthesis myConnection、myRequest、storeData;
+(StoreQuery*)查询ForStores:(id)aDelegate
{
StoreQuery*StoreQuery=[[[StoreQuery alloc]init]autorelease];
storeQuery.delegate=aDelegate;
storeQuery.myRequest=[NSURLRequest requestWithURL:@“URL”];
storeQuery.myConnection=[NSURLConnection connectionWithRequest:storeQuery.myRequest委托:storeQuery];
storeQuery.storeData=[[NSMutableArray数据]保留];
返回存储查询;
}
-(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应
{
[self.storeData setLength:0];
}
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
{
[self.storeData-appendData:data];
}
-(无效)连接:(NSURLConnection*)连接
didFailWithError:(n错误*)错误
{
NSLog(@“连接错误:%@,[Error localizedDescription]);
if(自授权){
[self.delegate请求:self.delegate错误:error];
}
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
if(自授权){
[\u委托请求:self didfishwithobject:self.storeData];
}
}
-(无效)解除锁定
{
[我的请求释放];
[连接释放];
[存储数据发布];
[super dealoc];
}

MVC架构的最佳示例是。
它是通过使用来实现的。

我倾向于消除整个
StoryQuery
类(这里您似乎并不真正需要任何
NSURLConnectionDataDelegate
方法),只需使用基于块的网络调用,使用标准的
NSURLConnection
或奇妙的
AFNetworking
框架

标准
NSURLConnection
技术:

-(void)queryStoreData
{
    NSURL *url = [NSURL URLWithString:@"yoururl"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:self.aQueue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                               if (error)
                               {
                                   NSLog(@"%s: error retrieving data = %@", __FUNCTION__, error);
                                   return;
                               }

                               // now parse the results, e.g., if it was JSON:

                               NSError *parseError = nil;
                               self.results = [NSJSONSerialization JSONObjectWithData:data
                                                                              options:0
                                                                                error:&parseError];
                               if (parseError)
                               {
                                   NSLog(@"%s: error parsing data = %@", __FUNCTION__, parseError);
                                   return;
                               }

                               // note, all user interface updates must happen on main queue, e.g.

                               [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                   [self.tableView reloadData];
                               }];
                           }];
}
或者使用
AFNetworking

-(void)queryStoreData
{
    NSURL *url = [NSURL URLWithString:@"yoururl"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^
                                         (NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        self.results = JSON;
        [self.tableView reloadData];
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"JSON network request failed: %@", error);
    }];
    [operation start];
}

顺便说一句,虽然我试图提供一些实际的例子来说明如何做到这一点,但您的问题提到了使用“MVC架构”。发出网络请求与适当的模型-视图-控制器体系结构之间的联系很薄弱。但是,如果您真的关心适当的MVC体系结构,您可能不会使结果数组成为控制器对象的成员,但是您可能实际使用了一些模型对象。但是我假设真正的问题是如何执行网络操作和使用结果,而不是MVC架构。答案应该不仅仅是一个链接,你应该给出链接所涵盖内容的描述,这样答案在没有链接的情况下仍然有效