这是执行异步块的一个基本问题,我还没有找到一个好的解决方案.
以下代码有效,但它阻止了主线程,我想避免这种情况.
override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
//check if transaction already exists
let trans = PFQuery(className: "Transactions")
trans.whereKey("itemId", equalTo: self.pfObject.objectId!)
//I need the count of objects in the query before I can proceed, but I don't want to block the main thread
let count = trans.countObjects()
if(count > 0){
return false
}else{
return true
}
}
Run Code Online (Sandbox Code Playgroud)
此问题并非特定于我的应用程序的这一部分.通常,我可以在query.countObjectsInBackGroundWithBlock()之类的闭包中设置"count"(或者我需要的任何变量),但是当我需要在主线程上返回一些东西时我不能这样做.
有没有一个解决方案让我的应用程序等待返回而不阻塞主线程?我实际上并不认为在这种情况下没有重新设计大部分代码,但我只是想确保我不是天真的.
对于这些类型的问题,有哪些可接受的解决方案?
我使用CLLocationCoordinate2D获取用户的当前位置,然后将其用于Parse的nearGeoPoint.
我知道我可以简单地使用geoPointForCurrentLocationInBackground但我宁愿用CLLocationCoordinate2D检索.
objective-c ios parse-platform geopoints cllocationcoordinate2d
我有一个数组 objectIds = ["LlbAXkqOL4", "v7lSgUunbR"]
如果我只有一个对象id,那么我可以使用,
query.getObjectInBackgroundWithId("A2332xsdas2")
由于我有很多objectIds,我遵循以下代码:
let userQuery = PFUser.query()
userQuery?.whereKey("username", equalTo: self.user!.username!)
userQuery?.findObjectsInBackgroundWithBlock {
(object, error) -> Void in
if object != nil
{
for messageObject in object! {
self.importedArray = ((messageObject as! PFObject)["AllEventsId"] as? [String]!)!
println("importedArray = \(self.importedArray)")
}
}
}
var query:PFQuery = PFQuery(className: "Events")
query.whereKey("objectId", containedIn: self.importedArray)
query.whereKey("EventSTDTime", greaterThan: zDate)
query.findObjectsInBackgroundWithBlock {
(object, error) -> Void in
if object != nil
{
println(objects)
}
}
Run Code Online (Sandbox Code Playgroud)
但是这个方法不起作用,因为它获取了类下的所有id Events
我的parse / AngularJS应用程序正在使用Promises。在登录功能是假设重定向与用户$ location.path,但我不得不调整它,使其根据该岗位工作:
在AngularJS中$ location.path不会在工厂中更改
根据这一SO用户,你需要将调用替换到$ location.path与
$rootScope.$apply( function(){$location.path('/somelocatin'); } );
Run Code Online (Sandbox Code Playgroud)
在这里查看答案:https : //stackoverflow.com/a/19738154/1743693
这解决了问题,但我必须了解:为什么本地电话不起作用?调整如何解决该问题?我怎么知道我没有搞乱AngularJS周期?还是与Promise模式的Parse实现更相关?
这是登录代码:
function login() {
vm.dataLoading = true;
var response = AuthenticationService.Login(vm.email, vm.password).then(
function (user) {
vm.dataLoading = false;
//$location.path('/'); -- <-- **doesn't work**
vm.user = Parse.User.current().getEmail();
$rootScope.$apply(function () { $location.path('/'); });
},
function () {
$rootScope.$apply(function () { $location.path('/login'); });
FlashService.Error(response.message);
vm.dataLoading = false;
});
};
Run Code Online (Sandbox Code Playgroud)
和身份验证服务的实现:
function Login(username, password, success, error) {
return Parse.User.logIn(username, …Run Code Online (Sandbox Code Playgroud) 我想将我从facebook获取的朋友列表ID转换为数组以保存在解析中.我的代码如下所示,但我得到一个"意外发现nil,同时解开一个Optional值"错误.如果需要,我该怎么做才能保存结果以解析并将其作为数组检索?
let fbRequest = FBSDKGraphRequest(graphPath:"/me/friends", parameters: nil);
fbRequest.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error == nil {
print("Friends are : \(result)")
if let dict = result as? Dictionary<String, AnyObject>{
let profileName:NSArray = dict["name"] as AnyObject? as! NSArray
let facebookID:NSArray = dict["id"] as AnyObject? as! NSArray
print(profileName)
print(facebookID)
}
}
else {
print("Error Getting Friends \(error)");
}
}
Run Code Online (Sandbox Code Playgroud)
当我在print()中使用下面的代码时,我得到以下结果:
Friends are : {
data = (
{
id = 138495819828848;
name = …Run Code Online (Sandbox Code Playgroud) 我想创建自定义PFObject类,但它给了我一个非常令人困惑的错误"属性self.photo未在super.init调用时初始化"
这是我的自定义类的代码:
import Foundation
import Parse
class Wish: PFObject, PFSubclassing {
var id: String?
var name: String?
var descriptionWish: String?
var photo: UIImage
var photoThumbnail: UIImage
var amount: Double?
var amountDonated: Int?
static func parseClassName() -> String {
return "Wish"
}
override init() {
super.init()
}
convenience init(id: String?, name: String?, descriptionWish: String?, photo: UIImage, photoThumbnail: UIImage, amount: Double?, amountDonated: Int?) {
self.init()
self.id = id
self.name = name
self.descriptionWish = descriptionWish
self.photo = photo
self.photoThumbnail = photoThumbnail
self.amount = …Run Code Online (Sandbox Code Playgroud) 我正在使用Parse,它在数据库中有一个preload User表.我希望每个用户都有一个唯一的userId(Int).Parse的objectId是唯一的,但不是Int,username是String.用户名对每个用户都是唯一的,所以我能以某种方式将每个用户名转换为一个数字吗?
我试过.toInt(),Int(),但我什么也没得到.
为什么:
我有一个包含用户评级(电影)的现有表格,我希望扩大此表格的评分范围.userId字段是Number值,所以我必须保持这种方式.
使用Swift和Parse,我需要获得与设备设置无关的日期和时间(UTC通用日期).NSDate()工作正常,但如果用户在设备设置中更改时区或时间或日期,则控制功能变得无用.基本上我必须检查一个用户每天最多可以向Parse数据库发送1张照片.目前要执行此操作,我搜索CurrentUser发送的最后1张照片,然后运行控件:if objectPhoto1 dates == NSDate {blah blah bla ...
问题是NSDate取决于设备的设置,因此如果用户更改日期可能会发送无限照片.
有没有办法从Parse服务器或Apple服务器或其他东西获取当前日期?
我觉得这很愚蠢,但它变成了一场噩梦!
也许我没有解释,我没有要发布的代码,因为如果我没有正确的日期和时间,这个功能就不存在了.实际例子:
// my iPhone current date and time (correct time) >> 10/12/2015 16:42:18
let TimeNow = NSDate()
print(TimeNow) // show "10/12/2015 16:42:18"
Run Code Online (Sandbox Code Playgroud)
如果我改变我的日期iPhone设置
// now my iPhone date and time are (incorrect time) >> 01/01/2002 15:30:26
let TimeNow = NSDate()
print(TimeNow) // show "01/01/2002 15:30:26"
Run Code Online (Sandbox Code Playgroud)
现在我的日期和时间都错了.我需要设置一个变量,其中包含设备设置的当前时间戳REGARDLESS.
我使用parse向iOS和Android用户发送推送.出于某种原因,在Parse界面更新后,默认设置为"递增应用徽章?" 已启用,所有iOS用户现在都会看到带有"1"的红色徽章.
在我目前的应用版本中.我的应用程序中没有删除徽章的任何代码.如何为我的用户重置此徽章?也许我可以发送一些推动设置它等于0?
push-notification apple-push-notifications ios parse-platform swift
我只是按照一个教程使用swift和parse制作了一个Facebook登录按钮。我按下了登录按钮,并首次成功登录。之后,当我再次运行该应用程序并再次按“登录”按钮时,我得到下图的屏幕: 登录页面
当我按“确定”时,一切都很好,然后转到下一个viewContoller,问题出在我按“取消”按钮后,应用程序崩溃了。你能给我一些提示为什么会发生吗?
import UIKit
import Parse
import ParseFacebookUtilsV4
import ParseTwitterUtils
var userName: String = ""
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@available(iOS 8.0, *)
@IBAction func signInButtonTapped(sender: AnyObject) {
PFFacebookUtils.logInInBackgroundWithReadPermissions([], block: { (user: PFUser?, error: NSError?) -> Void in
if (error != nil)
{
//Display an alert message
var myAlert = UIAlertController(title: "Alert", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction);
self.presentViewController(myAlert, animated: true, completion: nil)
return
}
if(FBSDKAccessToken.currentAccessToken() …Run Code Online (Sandbox Code Playgroud) parse-platform ×10
swift ×7
ios ×6
angularjs ×1
date ×1
facebook ×1
geopoints ×1
ios9 ×1
javascript ×1
nsdate ×1
objective-c ×1
pfobject ×1
swift2 ×1
timestamp ×1