我是iOS的新手.我正在研究应用程序需要运行一个任务,在后台线程中从服务器获取数据,因为我不想在主线程中锁定UI.这个任务需要很长时间,我尝试使用NSTimer,但它仍然锁定了UI.我的任务是在聊天屏幕中查看新消息,我需要每隔5秒调用一次此任务.如果我使用NSTimer,当输入文本时,文本似乎在此任务执行时冻结片刻.有没有办法在没有锁定UI的情况下处理此任务.请给我一些建议.非常感谢.
==更新代码==
- (void)performBackgroundTask
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Do background work
if([[NSUserDefaults standardUserDefaults] boolForKey:@"LoggedIn"]) {
NSDictionary * userDictionary = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"SessionDictionary"];
NSString *authenKey= [userDictionary valueForKey:@"authToken"];
NSString* limit = @"1000";
[[LSDataManager sharedDataManager] getLatestMessagesWithAuthKey:authenKey andLimit:limit withBlock:^ (NSDictionary* responseDict)
{
if (responseDict) {
[self loadDataFromServer:responseDict];
NSArray* lastMessageArray= nil;
//filter message data
if (self.MessagesArray.count >0) {
if (!self.isSeller) {
lastMessageArray = [self filterMessageData:self.MessagesArray withProductID:self.productID withSellerID:self.receiverID withBuyerID:self.senderID];
}
else
{
lastMessageArray = [self filterMessageData:self.MessagesArray withProductID:self.productID withSellerID:self.senderID withBuyerID:self.receiverID];
}
NSLog(@"filter array %@",lastMessageArray);
if([lastMessageArray count] >0){ …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我添加了1 UICollectionview并用于UIImagePickerController拍照然后上传到服务器,collectionview用于显示所有照片.但是当我点击我的应用程序中的"相机"按钮打开相机时,我Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.在控制台日志中收到了警告.有时相机屏幕是黑屏.我试过搜索,但我无法解决这个问题.你能帮我解决这个问题吗?提前致谢.
iphone objective-c uiimagepickercontroller ios uicollectionview
我有一个子视图,并添加了1个按钮.我想在该视图的中心添加此按钮.我使用Autolayout所以我需要以编程方式设置此按钮的约束.
我试过这段代码,但播放按钮不在中心.
[self.moviePlayer.view addSubview:self.playbtn];
self.playbtn.translatesAutoresizingMaskIntoConstraints = NO;
[self.moviePlayer.view addConstraint:[NSLayoutConstraint constraintWithItem:self.playbtn attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual
toItem:self.moviePlayer.view attribute:NSLayoutAttributeCenterX multiplier:0.667 constant:0]];
Run Code Online (Sandbox Code Playgroud)
请帮我纠正一下.提前致谢.