在绘制之前从DB获取数据

时间:2014-05-04 15:47:14

标签: ios objective-c sql-server

我正在开发应用程序,它应该通过freetds连接到MSSQL DB,从db获取数据然后显示这些数据。显示数据并从数据库中获取它可以单独工作,但是当我尝试组合它们时,负责从db获取数据的代码总是在应该绘制此数据的代码之后运行。可能有人解释我错了。

KSOAppDelegate.m

#import "KSOAppDelegate.h"
#import "KSOCharViewController.h"

@implementation KSOAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    KSOCharViewController *cvc = [[KSOCharViewController alloc]init];

    self.window.rootViewController = cvc;

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

KSOCharViewController.m

#import "KSOCharViewController.h"
#import "KSOChartView.h"

@implementation KSOCharViewController
-(void)loadView
{
    KSOChartView *mainView = [[KSOChartView alloc]init];
    self.view = mainView;
}

KSOChartView.m

- (void)drawRect:(CGRect)rect
{
SQLClient* client = [SQLClient sharedInstance];
    client.delegate = self;
    [client connect:@"10.146.1.28:1433" username:@"sa" password:@"password" database:@"Intraservice4" completion:^(BOOL success) {
                if (success)
                {
                        [client execute:@"SELECT u.Name,count(t.Id) as tot FROM [Intraservice4].[dbo].[Task] t left join TaskExecutor te on t.id = te.TaskId left join [User] u on te.UserId = u.id where 1=1 and StatusId not in ( 28,29,30,53) group by u.Name order by u.Name" completion:^(NSArray* results) {
                                //NSLog(@"%@",[results[0][0] valueForKey:@"tot"]);
                NSLog(@"THIS SHOULD BE THE FIRST BUT ITS NOT!!!!");
                //[self process:results];
                [client disconnect];
                        }];
                }
        }];

       NSLog(@"THIS SHOUD BE THE SECOND BUT ITS NOT");
}

1 个答案:

答案 0 :(得分:1)

您的日志NSLog(@"THIS SHOUD BE THE SECOND BUT ITS NOT");位于完成块之外,因此它会在您调用connect:username:password:database:completion:后立即运行,因为该方法未阻止(它会立即返回并在调用时completion异步操作完成。)

所以,你需要移动你的第二个'逻辑进入completion块,或者,最好从块中调用一个方法来运行你的第二个'逻辑。