Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
openbis
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sispub
openbis
Commits
e49acba0
Commit
e49acba0
authored
12 years ago
by
cramakri
Browse files
Options
Downloads
Patches
Plain Diff
Synch the cache in a secondary thread.
SVN: 27449
parent
b6963f1e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
openbis-ipad/BisKit/Classes/CISDOBIpadServiceManager.m
+114
-48
114 additions, 48 deletions
openbis-ipad/BisKit/Classes/CISDOBIpadServiceManager.m
with
114 additions
and
48 deletions
openbis-ipad/BisKit/Classes/CISDOBIpadServiceManager.m
+
114
−
48
View file @
e49acba0
...
...
@@ -38,14 +38,34 @@
@
end
static
NSManagedObjectContext
*
GetDatabaseManagedObjectContext
(
NSURL
*
storeUrl
,
NSError
**
error
)
//
Internal
class
that
synchronizes
result
data
to
the
managed
object
context
@
interface
CISDOBBackgroundDataSynchronizer
:
NSObject
@
property
(
readonly
,
weak
)
CISDOBIpadServiceManager
*
serviceManager
;
@
property
(
readonly
)
CISDOBIpadServiceManagerCall
*
managerCall
;
@
property
(
readonly
)
NSArray
*
rawEntities
;
@
property
(
readonly
)
NSManagedObjectContext
*
managedObjectContext
;
@
property
(
readonly
)
NSError
*
error
;
@
property
(
nonatomic
)
BOOL
prune
;
//
Initialization
-
(
id
)
initWithServiceManager
:(
CISDOBIpadServiceManager
*
)
serviceManager
managerCall
:(
CISDOBIpadServiceManagerCall
*
)
call
rawEntities
:(
NSArray
*
)
rawEntities
;
//
Actions
-
(
void
)
run
;
-
(
void
)
notifyCallOfResult
:(
id
)
args
;
@
end
static
NSManagedObjectContext
*
GetMainThreadManagedObjectContext
(
NSURL
*
storeUrl
,
NSError
**
error
)
{
//
Explicitly
specify
which
db
schema
we
want
to
use
NSBundle
*
bundle
=
[
NSBundle
bundleForClass
:
[
CISDOBIpadEntity
class
]];
NSString
*
momPath
=
[
bundle
pathForResource
:
@
"persistent-data-model"
ofType
:
@
"momd"
];
NSManagedObjectModel
*
mom
=
[[
NSManagedObjectModel
alloc
]
initWithContentsOfURL
:
[
NSURL
fileURLWithPath
:
momPath
]];
NSManagedObjectContext
*
moc
=
[[
NSManagedObjectContext
alloc
]
init
];
NSManagedObjectContext
*
moc
=
[[
NSManagedObjectContext
alloc
]
init
WithConcurrencyType
:
NSMainQueueConcurrencyType
];
NSPersistentStoreCoordinator
*
coordinator
=
[[
NSPersistentStoreCoordinator
alloc
]
initWithManagedObjectModel
:
mom
];
[
moc
setPersistentStoreCoordinator
:
coordinator
];
NSPersistentStore
*
store
=
...
...
@@ -72,7 +92,7 @@ static NSManagedObjectContext* GetDatabaseManagedObjectContext(NSURL* storeUrl,
CISDOBLiveConnection
*
connection
=
[[
CISDOBLiveConnection
alloc
]
initWithUrl
:
openbisUrl
trusted
:
trusted
];
_
storeUrl
=
[
storeUrl
copy
];
_
service
=
[[
CISDOBIpadService
alloc
]
initWithConnection
:
connection
];
_
managedObjectContext
=
Get
Database
ManagedObjectContext
(
self
.
storeUrl
,
error
);
_
managedObjectContext
=
Get
MainThread
ManagedObjectContext
(
self
.
storeUrl
,
error
);
_
persistentStoreCoordinator
=
_
managedObjectContext
.
persistentStoreCoordinator
;
if
(
!_
managedObjectContext
)
return
nil
;
...
...
@@ -82,45 +102,16 @@ static NSManagedObjectContext* GetDatabaseManagedObjectContext(NSURL* storeUrl,
return
self
;
}
-
(
BOOL
)
sync
h
Entit
y
:(
CISDOBIpadRawEntity
*
)
rawEntity
lastUpdateDate
:(
NSDate
*
)
date
error
:(
NSError
**
)
error
-
(
void
)
syncEntit
ies
:(
NSArray
*
)
rawEntities
pruning
:(
BOOL
)
prune
notifying
:(
CISDOBIpadServiceManagerCall
*
)
managerCall
{
//
Create
new
entities
in
the
moc
,
and
store
them
.
CISDOBIpadEntity
*
entity
;
NSArray
*
matchedEntities
=
[
self
entitiesByPermId
:
[
NSArray
arrayWithObject
:
rawEntity
.
permId
]
error
:
error
];
if
(
!
matchedEntities
)
return
NO
;
if
([
matchedEntities
count
]
>
0
)
{
entity
=
[
matchedEntities
objectAtIndex
:
0
];
[
entity
updateFromRawEntity
:
rawEntity
];
}
else
{
entity
=
[
NSEntityDescription
insertNewObjectForEntityForName
:
@
"CISDOBIpadEntity"
inManagedObjectContext
:
self
.
managedObjectContext
];
[
entity
initializeFromRawEntity
:
rawEntity
];
}
entity
.
lastUpdateDate
=
date
;
entity
.
serverUrlString
=
[((
CISDOBLiveConnection
*
)(
self
.
service
.
connection
))
.
url
absoluteString
];
return
YES
;
}
-
(
BOOL
)
syncEntities
:(
NSArray
*
)
rawEntities
pruning
:(
BOOL
)
prune
error
:(
NSError
**
)
error
{
NSDate
*
lastUpdateDate
=
[
NSDate
date
];
BOOL
success
;
for
(
CISDOBIpadRawEntity
*
rawEntity
in
rawEntities
)
{
success
=
[
self
synchEntity
:
rawEntity
lastUpdateDate
:
lastUpdateDate
error
:
error
];
if
(
!
success
)
return
NO
;
}
//
If
pruning
is
requested
,
remove
entities
that
cannot
be
reached
from
the
server
result
set
.
//
TODO
:
we
should
treat
the
intial
results
as
a
root
set
and
trace
out
to
do
a
gc
,
but
the
simpler
implementation
is
just
to
remove
everything
that
is
not
mentioned
if
(
prune
)
{
//
Remove
all
entities
that
were
not
mentioned
NSArray
*
entitiesToDelete
=
[
self
entitiesNotUpdatedSince
:
lastUpdateDate
error
:
error
];
for
(
CISDOBIpadEntity
*
entity
in
entitiesToDelete
)
{
[
self
.
managedObjectContext
deleteObject
:
entity
];
}
}
success
=
[
self
.
managedObjectContext
save
:
error
];
return
success
;
void
(
^
syncBlock
)(
void
)
=
^
{
CISDOBBackgroundDataSynchronizer
*
synchronizer
=
[[
CISDOBBackgroundDataSynchronizer
alloc
]
initWithServiceManager
:
self
managerCall
:
managerCall
rawEntities
:
rawEntities
];
synchronizer
.
prune
=
prune
;
[
synchronizer
run
];
[
synchronizer
performSelectorOnMainThread
:
@
selector
(
notifyCallOfResult
:)
withObject
:
nil
waitUntilDone
:
NO
];
};
NSBlockOperation
*
blockOp
=
[
NSBlockOperation
blockOperationWithBlock
:
syncBlock
];
[
blockOp
start
];
}
-
(
CISDOBIpadServiceManagerCall
*
)
managerCallWrappingServiceCall
:(
CISDOBAsyncCall
*
)
serviceCall
pruning
:(
BOOL
)
prune
...
...
@@ -129,13 +120,7 @@ static NSManagedObjectContext* GetDatabaseManagedObjectContext(NSURL* storeUrl,
serviceCall
.
success
=
^
(
id
result
)
{
//
Update
the
cache
NSError
*
error
;
BOOL
didSync
=
[
self
syncEntities
:
result
pruning
:
prune
error
:
&
error
];
if
(
!
didSync
)
{
serviceCall
.
fail
(
error
);
}
else
if
(
managerCall
.
success
)
{
managerCall
.
success
(
result
);
}
[
self
syncEntities
:
result
pruning
:
prune
notifying
:
managerCall
];
};
serviceCall
.
fail
=
^
(
NSError
*
error
)
{
if
(
managerCall
.
fail
)
managerCall
.
fail
(
error
);
};
...
...
@@ -224,3 +209,84 @@ static NSManagedObjectContext* GetDatabaseManagedObjectContext(NSURL* storeUrl,
}
@
end
@
implementation
CISDOBBackgroundDataSynchronizer
//
Initialization
-
(
id
)
initWithServiceManager
:(
CISDOBIpadServiceManager
*
)
serviceManager
managerCall
:(
CISDOBIpadServiceManagerCall
*
)
call
rawEntities
:(
NSArray
*
)
rawEntities
{
if
(
!
(
self
=
[
super
init
]))
return
nil
;
_
serviceManager
=
serviceManager
;
_
managerCall
=
call
;
_
rawEntities
=
rawEntities
;
_
managedObjectContext
=
[[
NSManagedObjectContext
alloc
]
initWithConcurrencyType
:
NSConfinementConcurrencyType
];
_
managedObjectContext
.
parentContext
=
_
serviceManager
.
managedObjectContext
;
_
prune
=
NO
;
_
error
=
nil
;
return
self
;
}
-
(
BOOL
)
synchEntity
:(
CISDOBIpadRawEntity
*
)
rawEntity
lastUpdateDate
:(
NSDate
*
)
date
error
:(
NSError
**
)
error
{
//
Create
new
entities
in
the
moc
,
and
store
them
.
CISDOBIpadEntity
*
entity
;
NSArray
*
matchedEntities
=
[
self
.
serviceManager
entitiesByPermId
:
[
NSArray
arrayWithObject
:
rawEntity
.
permId
]
error
:
error
];
if
(
!
matchedEntities
)
return
NO
;
if
([
matchedEntities
count
]
>
0
)
{
entity
=
[
matchedEntities
objectAtIndex
:
0
];
[
entity
updateFromRawEntity
:
rawEntity
];
}
else
{
entity
=
[
NSEntityDescription
insertNewObjectForEntityForName
:
@
"CISDOBIpadEntity"
inManagedObjectContext
:
self
.
managedObjectContext
];
[
entity
initializeFromRawEntity
:
rawEntity
];
}
entity
.
lastUpdateDate
=
date
;
entity
.
serverUrlString
=
[((
CISDOBLiveConnection
*
)(
self
.
serviceManager
.
service
.
connection
))
.
url
absoluteString
];
return
YES
;
}
-
(
void
)
run
{
NSError
*
error
;
NSDate
*
lastUpdateDate
=
[
NSDate
date
];
BOOL
success
;
for
(
CISDOBIpadRawEntity
*
rawEntity
in
self
.
rawEntities
)
{
success
=
[
self
synchEntity
:
rawEntity
lastUpdateDate
:
lastUpdateDate
error
:
&
error
];
if
(
!
success
)
{
_
error
=
[
error
copy
];
return
;
}
}
//
If
pruning
is
requested
,
remove
entities
that
cannot
be
reached
from
the
server
result
set
.
//
TODO
:
we
should
treat
the
intial
results
as
a
root
set
and
trace
out
to
do
a
gc
,
but
the
simpler
implementation
is
just
to
remove
everything
that
is
not
mentioned
if
(
_
prune
)
{
//
Remove
all
entities
that
were
not
mentioned
NSArray
*
entitiesToDelete
=
[
self
.
serviceManager
entitiesNotUpdatedSince
:
lastUpdateDate
error
:
&
error
];
for
(
CISDOBIpadEntity
*
entity
in
entitiesToDelete
)
{
[
self
.
managedObjectContext
deleteObject
:
entity
];
}
}
success
=
[
self
.
managedObjectContext
save
:
&
error
];
if
(
!
success
)
{
_
error
=
[
error
copy
];
return
;
}
_
error
=
nil
;
}
-
(
void
)
notifyCallOfResult
:(
id
)
args
{
if
(
_
error
)
{
self
.
managerCall
.
serviceCall
.
fail
(
self
.
error
);
}
else
if
(
self
.
managerCall
.
success
)
{
self
.
managerCall
.
success
(
self
.
rawEntities
);
}
}
@
end
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment