2

I have the following design pattern query problem. I have a web service in PHP from which I want to perform, log in authorisation, and SQL operations, some with NSDictionary responses, and some with BOOL responses.

I have had a search for design patterns and hit this: Best architectural approaches for building iOS networking applications (REST clients)

Now although this post is very interesting, I don't think I need anything as complicated. I have started with a singleton object as a assetDBConnection (which is self.delegate in the below code], but I don't know whether this is the best idea.

The only way I can think of having my assetDBConnection abstracted away from my APIWebRetrieve data retriever is to pull out and compare strings from the response. This would mean grouping the responses at the PHP service level into types, e.g. type="logon",etc,etc.

-(void)APIWebRetrieveFinished:(APIWebRetrieve *)api
{
    NSLog(@"api:%@",[api description]);
    NSLog(@"%@",api.dataString);
    NSError *error;
    if (api.dataString) {
        NSDictionary *results=[NSJSONSerialization JSONObjectWithData:[api.dataString dataUsingEncoding:NSStringEncodingConversionAllowLossy]
                                    options:NSJSONReadingMutableContainers
                                      error:&error];

        if ([[results objectForKey:@"type"] isEqualToString:@"logon"]){
            if ([results objectForKey:@"response"]){
                [self.delegate loginOK];
            }else{
                [self.delegate loginFail];
            }
       }
    }
}

Any help appreciated.

Update: Ok, this must be pretty dull. No repsonses. I have started to add another layer in, called services. I quite like the fact that I now have this sort of code popping up:

#import "AssetDBConnection.h"
#import "AssetDBServiceLogon.h"

@implementation AssetDBConnection

-(BOOL)loginWithUsername:(NSString*)user andPassword:(NSString*)password
{
    [AssetDBService logonWithUsername:user password:password andDelegate:self];
}

I just hope this is going in the right direction!

Community
  • 1
  • 1
Simon Unsworth
  • 321
  • 1
  • 3
  • 8

1 Answers1

1

Tumbleweed!

Hence why I though I might as well post my own answer. i.e. The implementation details of what I did.

There is an APIWebRetrieve class which does a POST web call for all microservices (?? I'm not too sure this is the correct terminology) e.g. GetAssetDetails, MoveAsset, CreateFaultForAsset. The POST call has a operation type e.g.="asset-details", and returns a JSON object (converted to NSDictionary) to the GetAssetDetails object which then returns the data contents of the key which has the same name as the original call type. Then I return this back up to an AssetDBConnection Singleton, which then passes this back to the delegate of the singleton DBConnection class.

I will try and draw this...

                     CallingViewController
                              |
                              |
                              |
  ------------AssetDBConnection (Singleton)-------------
  |            |          |         |                  |
GetAssetDetail  AssetFault   OtherMS  OtherMS           OtherMS  
Instance       Instance    Instance  Instance        Instance
  |              |              |         |              |
APIWebretrieve APIWebretrieve APIWeb....  APIWeb...   APIWeb...
Instance         Instance        Instance    Instance    Instance

The | character pretty much represents the top class is a delegate of the lower.

How does this implementation look? (Whooops a question in an answer, I'll get shot down)

Well regardless of being an answer, this is myAnswer.

Cheers

Simon Unsworth
  • 321
  • 1
  • 3
  • 8