我正在测试一些使用Grand Central Dispatch进行异步处理的代码.测试代码如下所示:
[object runSomeLongOperationAndDo:^{
STAssert…
}];
Run Code Online (Sandbox Code Playgroud)
测试必须等待操作完成.我目前的解决方案如下:
__block BOOL finished = NO;
[object runSomeLongOperationAndDo:^{
STAssert…
finished = YES;
}];
while (!finished);
Run Code Online (Sandbox Code Playgroud)
看起来有点粗糙,你知道更好的方法吗?我可以通过调用暴露队列然后阻塞dispatch_sync:
[object runSomeLongOperationAndDo:^{
STAssert…
}];
dispatch_sync(object.queue, ^{});
Run Code Online (Sandbox Code Playgroud)
......但是这可能会暴露太多object.
我创建了一个像这样的用户数组 -
var ref = new Firebase(FIREBASE_URL + '/users');
var users = $firebaseArray(ref);
Run Code Online (Sandbox Code Playgroud)
我已经添加了对象,我想循环遍历此数组中的每个用户.我知道在使用ng-repeat的视图中这是可能的,但我需要在控制器中执行此操作.我试过了 -
angular.forEach(users, function(user) {
console.log(user);
})
Run Code Online (Sandbox Code Playgroud)
但是我没有得到这个结果.
这更像是一个JavaScript Closure问题而不是Firebase问题.在以下代码中,Firebase回调未识别父作用域中的变量myArr.
function show_fb() {
var myArr = [];
var firebase = new Firebase('https://scorching-fire-6816.firebaseio.com/');
firebase.on('child_added', function(snapshot) {
var newPost = snapshot.val();
myArr.push(newPost.user);
console.log(myArr); // works
});
console.log(myArr); // doesn't work. myArr in the firebase.on callback is
// not altering myArr
return myArr;
};
Run Code Online (Sandbox Code Playgroud) 我有一个firebase Auth工厂如下
app.factory("Auth", ["$firebaseAuth", "FIREBASE_URL","$ionicPlatform",
function($firebaseAuth, FIREBASE_URL, $ionicPlatform) {
var auth = {};
$ionicPlatform.ready(function(){
var ref = new Firebase(FIREBASE_URL);
auth = $firebaseAuth(ref);
});
return auth;
}
]);
Run Code Online (Sandbox Code Playgroud)
我在我的ui-router解决方案中注入Auth工厂作为依赖关系但是在配置ui-router时,auth是空的,因为平台准备就绪后开始.
app.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('menu', {
url: '/menu',
abstract:true,
cache: false,
controller: 'MenuCtrl',
templateUrl: 'templates/menu.html',
resolve: {
auth: function($state, Auth){
//<--Auth is empty here when the app starts ----->
return Auth.$requireAuth().catch(function(){
$state.go('login'); //if not authenticated send back to login
});
}
}
})
Run Code Online (Sandbox Code Playgroud)
如何确保Auth工厂在注入ui-router之前不为空?
我试过这个 -
app.factory("Auth", ["$firebaseAuth", "FIREBASE_URL","$ionicPlatform",
function($firebaseAuth, FIREBASE_URL, …Run Code Online (Sandbox Code Playgroud) firebase ×3
angularjs ×2
javascript ×2
ajax ×1
angularfire ×1
closures ×1
ionic ×1
objective-c ×1
unit-testing ×1