ios


Need to share data from (A) thread to (B) Thread using +ios (objective c or swift)


I need to share data from 1 thread to another running thread. it's possible in ios(objective c or swift). If its possible then anyone please suggest me.
I have A-thread & B-Thread both are running parallel.
In A-thread have (XYZ Object).
Need to pass on B-Thread in running process.
Suggest me best solution or any blogs, tutorial, example etc....
Both A and B can refer to a class containing this variable. You can then make this variable volatile, and this means that changes to that variable are immeditately visible in both threads.
See this article for more info
Volatile variables share the visibility features of synchronized, but none of the atomicity features. This means that threads will automatically see the most up-to-date value for volatile variables. They can be used to provide thread safety, but only in a very restricted set of cases: those that do not impose constraints between multiple variables or between a variable's current value and its future values.
You have to create a model class of type NSObject. this model class will contain data as a singleton object. that singleton object can be accessed from anywhere by retaining the data within different threads. You atomic property, if you want to make thread safe reading.
You can do it using dispatch_queue aka Grand Central Dispatch (GCD) , Refer ConcurrencyProgrammingGuide.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Background work
//Here you will get Object A , lets say Object A as `UIImage`
//For example
NSURL *imageURL = [NSURL URLWithString:#"http://example.com/1.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
dispatch_async(dispatch_get_main_queue(), ^{
// Update UI
UIImage *image = [UIImage imageWithData:imageData];
//Here, lets say UIImageView is Object B
//Now Pass Object A to Object B like as follow
self.imageView.image = self.image;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Background work
dispatch_async(dispatch_get_main_queue(), ^{
// Update UI
});
});
});
});
OR
Assume you have two threads, threadX and threadY, each with one object, objectA and objectB. Do something like:
char buffer[100] = "Hello";
NSData *data = [NSData dataWithBytes:byBuffer length:100];
[objectY performSelector:#selector(haveSomeData:) onThread:threadY withObject:data waitUntilDone:NO modes:[NSArray arrayWithObject:NSDefaultRunLoopMode]];

Related Links

AVAnimator Assertion failure in -[AVMvidFrameDecoder advanceToFrame:] 'framebuffer num bytes'
Instagram InstagramCaption not working
Delete sections without scroll down
Facebook Login Button Throws An Error And Says I Cannot Login
In-app purchase receipt validation, “in_app” field empty
How can we pass the parameters along with block methods in objecitve-c
Apple Remote Push Notifications
Game stops working when restart button clicked
Swift - Create a custom push segue
how to set alpha on Backgroung image using Swift
Livecode revdberr invalid database connection
How to sort an array which contains objects with chars and decimals? [duplicate]
Set the selected Cell without pushing to new viewController
Page view constraints bug
How to rotate and crop iOS CGImage from file
How do I get the value of a UITextfield as characters are being typed in real time?

Categories

HOME
sbt
ssl
embedded-linux
codenameone
pyqt5
format
sony
tin-can-api
hadoop2
push
lag
aar
amplitude
orientdb-2.1
thrift
google-finance
pydev
multiprocessing
py4j
jcrop
selenium-edgedriver
confidence-interval
icloud-drive
launchpad
gecko
stackexchange.exceptional
lifecycle
jdk1.6
rhel7
delete-row
mask
folly
ubuntu-server
confirm
decompiler
disqus
cookiestore
caldroid
rft
uitabbarcontroller
es6-promise
plasticscm
gradle-tooling-api
quicksand
appdynamics
fido
koala-framework
easyhook
angular-directive
dr-memory
mailcore2
post-increment
sqlfiddle
finalcut
enhanced-ecommerce
xlform
pddl
yapdatabase
balana
php-opencloud
translucency
alienvault
size-t
kue
physx
django-postgresql
directcompute
zeroclipboard
pkcs#10
xacml3
nine-patch
delphi-2009
simpleaudioengine
sysadmin
programmers-notepad
nspersistentdocument
word-2010
java-ee-5
harvest
ad-hoc-distribution
working-directory
ramaze
demoscene
bubble-popup
emacs-ecb
openfeint
jtemplates
staging
myisam
pinging
net-ssh
utility
posterous
user-testing

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App