- (NSArray*) calendarMonthView:(TKCalendarMonthView*)monthView marksFromDate:(NSDate*)startDate toDate:(NSDate*)lastDate{
//SOAP Request has NSURLConnection which runs asychonrous delegate methods.
//SOAP Request will return before the data array has been populated
[self SOAPRequest:startDate];
//i need to do something like this, however the SOAPRequest will return and get stuck into the loop instead of the asychronos delegates firing
//therefore i can never set bGettingData to NO.
int iWait;
while (bGettingData) {
iWait++;
}
return dataArray;
}
您好,
在我正在创建的应用程序中,我依靠SOAP请求来检索数据,解析XML并填充数组。
我遇到的问题是,当我检查数组时它是空的,因为SOAP请求还没有完成。在SOAP请求完成并恢复代码之前,如何阻止我的代码执行?这可以通过回调或线程来完成吗?
由于
答案 0 :(得分:1)
不要暂时停止,睡觉或等待,而只是从当前的例程/功能/方法中退出/退出/返回。
将当前的“东西”分解为多个代码片段,每个片段都有自己的方法。
使用后续方法执行接下来的任何操作,并让异步网络/ SOAP请求的完成例程调用该方法。
基本上,您的问题是您仍然在考虑程序编码。正确的范例是使用事件驱动的编码:让操作系统调用您的代码,而不是让您的代码调用操作系统并等待。
答案 1 :(得分:0)
答案 2 :(得分:0)
您确实希望等待答案完成 - 回调通常最简单。究竟如何取决于您正在使用的编程库/语言(在javascript,objectiveC中,您是手工编写代码还是从示例开始)。
查看Iphone SOAP request step by step tutorial的答案 - 例如http://macresearch.org/interacting-soap-based-web-services-cocoa-part-1和http://brismith66.blogspot.com/2010/05/iphone-development-accesing-soap.html。或者按照https://developer.omniture.com/node/321 - 等待答案完全到达。
答案 3 :(得分:0)
不幸的是,在使用TapKu日历时,您无法通过SOAP从数据库异步加载。您必须同步加载日历,因为一旦数据加载完毕,它们就无法刷新日历视图。如果您每月有40多条记录,这将产生5-6秒的巨大延迟。
答案 4 :(得分:0)
这确实是可行的,作为日历视图的示例,将_refreshDataPageWithAtIndex修改为如下:
- (void) _refreshDataWithPageAtIndex:(NSInteger)index{
UIScrollView *sv = self.pages[index];
TKTimelineView *timeline = [self _timelineAtIndex:index];
CGRect r = CGRectInset(self.horizontalScrollView.bounds, HORIZONTAL_PAD, 0);
r.origin.x = self.horizontalScrollView.frame.size.width * index + HORIZONTAL_PAD;
sv.frame = r;
timeline.startY = VERTICAL_INSET;
for (UIView* view in sv.subviews) {
if ([view isKindOfClass:[TKCalendarDayEventView class]]){
[self.eventGraveYard addObject:view];
[view removeFromSuperview];
}
}
if(self.nowLineView.superview == sv) [self.nowLineView removeFromSuperview];
if([timeline.date isTodayWithTimeZone:self.timeZone]){
NSDate *date = [NSDate date];
NSDateComponents *comp = [date dateComponentsWithTimeZone:self.timeZone];
NSInteger hourStart = comp.hour;
CGFloat hourStartPosition = hourStart * VERTICAL_DIFF + VERTICAL_INSET;
NSInteger minuteStart = round(comp.minute / 5.0) * 5;
CGFloat minuteStartPosition = roundf((CGFloat)minuteStart / 60.0f * VERTICAL_DIFF);
CGRect eventFrame = CGRectMake(self.nowLineView.frame.origin.x, hourStartPosition + minuteStartPosition - 5, NOB_SIZE + self.frame.size.width - LEFT_INSET, NOB_SIZE);
self.nowLineView.frame = eventFrame;
[sv addSubview:self.nowLineView];
}
if(!self.dataSource) return;
timeline.events = [NSMutableArray new];
[self.dataSource calendarDayTimelineView:self eventsForDate:timeline.date andEvents:timeline.events success:^{
[timeline.events sortUsingComparator:^NSComparisonResult(TKCalendarDayEventView *obj1, TKCalendarDayEventView *obj2){
return [obj1.startDate compare:obj2.startDate];
}];
[self _realignEventsAtIndex:index];
if(self.nowLineView.superview == sv)
[sv bringSubviewToFront:self.nowLineView];
}];
}
然后将eventForDate函数更改为:
- (void) calendarDayTimelineView:(TKCalendarDayView*)calendarDayTimeline eventsForDate:(NSDate *)eventDate andEvents:(NSMutableArray *)events success:(void (^)())success {
[Model doSomethingAsync andSuccess:^(NSArray *classes) {
// .. Add stuff to events..
success();
}];
}
我假设其他控件的模式非常相似。前提是您正在等待继续格式化/布局流程,直到获得数据为止。